This is for an assignment in Haskell. We have been tasked with defining various functions using the foldr function.
We have been given a type:
group :: Eq a => [a] -> [[a]]
and been asked to define it such that:
group [1,2,2,3,4,4,4,5] = [[1], [2,2], [3], [4,4,4], [5]]
group [1,2,2,3,4,4,4,5,1,1,1] = [[1], [2,2], [3], [4,4,4], [5], [1,1,1]]
This is what I have so far:
group = foldr (\x xs -> if x == head (head xs) then (x : head xs) : xs else (x : []) : (head xs) : xs )
But when I try to load this into ghci interpreter I get the following error message:
Couldn't match type `[a0] -> [a]' with `[[a]]'
Expected type: [a] -> [[a]]
Actual type: [a] -> [a0] -> [a]
In the return type of a call of `foldr'
Probable cause: `foldr' is applied to too few arguments
In the expression:
foldr
(\ x xs
-> if x == head (head xs) then
(x : head xs) : xs
else
(x : []) : (head xs) : xs)
In an equation for `group':
group
= foldr
(\ x xs
-> if x == head (head xs) then
(x : head xs) : xs
else
(x : []) : (head xs) : xs)
If anyone could explain any reasons why my code isn't working as I expect it to, that would be greatly appreciated. Thanks.
group [];) (but beware there will be other issues ...) - Random Dev