Let's say I have a date type like this one:
type GroupNb = Int
type Code = Int
type Name = String
type Random = Int
data Group = Group GroupNb Code Name Random deriving Show
And I "add" some data(groups) in it.
Now, I want to get a list of all the GroupNb
having 1
as the Code
, and "test"
as a Name
.
groupNumbers :: [Group] -> [a]
groupNumbers (Group _ a b _) = nub[List.find (a == 1 && b == "test") [GroupNb]]
This solution doesn't work... How could I do something like that ?
If I have understood your problem correctly, you have a list of Group
that you want to filter based on a predicate (code == 1 && name == "test"
). That is exactly what the filter
function is for. So this is what your code looks like:
someTest :: Group -> Bool
someTest (Group _ code name _) = code == 1 && name == "test"
allGroupPassingTest :: [Group] -> [Group]
allGroupPassingTest g = filter someTest g
The helper method someTest
is the predicate that filter
is using.
Your original code was quite broken, because in haskell functions must start with a lower case. You're equality test was also broken, because =
is for assignment, not testing equality (you're looking for ==
). I have no idea what the square brackets are doing in here nub[List.find (a = 1 && b = "test") [GroupNb]]
. The only use of square brackets (to my knowledge) is to represent a list, or a list type. [GroupNb]
looks like you are trying to cast your list to this type, but the way to cast is using statement :: TypeCast
. List.find (a = 1 && b = "test")
is almost right, but it (a = 1 && b = "test")
should be written as a lambda like this (\(Group _ code name _) -> code == 1 && name == "test")
.
GroupNumbers :: Group -> [a]
GroupNumbers (Group _ a b _) = nub[List.find (a = 1 && b = "test") [GroupNb]]