The following code represents a function which takes a list as an input and then combines subsequent repetitions of the same value into a sublist. for example : pack ['a','a','b','b','b','a'] = [['a','a'], ['b','b','b'], ['a']]
pack :: [Char] -> [[Char]]
pack (x:xs) = ys : pack zs
where
(ys, zs) = takeall x (x:xs)
takeall _ [] = ([], [])
takeall x (y:ys)
| x == y = let (us, vs) = takeall x ys
in (y:us, vs)
| otherwise = ([], (y:ys))
I found this code in sample solution. I'm still a beginner at haskell but i've seen the similar problem once as an example for an algorithm in C language.while solving this problem it came to my mind to write the code with the help of higher order functions.I thought the combination of foldl,filter and concatMap could be a good solution for this problem, however i'm not quite sure about filter.I wanted to write a function for filter which goes through the list and compares each element with it's neighbour elements and if they are eqaul returns true. Writing a function like that could be easy but how should i combine it with other functions of higher order so the things that are true come to a list together? How do you solve this problem with higher order functions?
zipwith the list and its tail and thenfilteron the 2-tuples.