kreta
January 11, 2022, 5:47pm
1
Hi, a small question about some HoFs
I got a function, split, that uses recursion
it works as follows:
split ‘a’, “amandesas” = [“m”, "ndes, “s”]
How can I write this using HoF?
filter (/= a) xs kinda gets in the direction but isnt quite it
thanks for reading
f-a
January 11, 2022, 6:08pm
2
This might come handy:
λ> :m +Data.List
λ> :t groupBy
groupBy :: (a -> a -> Bool) -> [a] -> [[a]]
kreta
January 11, 2022, 6:20pm
3
oo thankss that looks real handy
kreta
January 11, 2022, 6:31pm
4
map (filter (/= ‘a’))( groupBy (/=) “alabamasong”)
this work! thank you for introducing groupBy lol
1 Like
jaror
January 11, 2022, 7:38pm
5
Unfortunately that is not a solution, e.g. try the input: “blabamasong”, and see what the output is (I think it is not what you want).
The behavior of groupBy
can be very strange if its first argument is not a proper equivalence relation (/=
is not an equivalence relation).
I would suggest using more traditional functions like span :: (a -> Bool) -> [a] -> ([a], [a])
or break :: (a -> Bool) -> [a] -> ([a], [a])
in a recursive function to solve this exercise.
If you really want to use groupBy
then an equivalence relation that you can use is (==) `on` (== 'a')
(with on
from Data.Function
) or equivalently \x y -> (x == 'a') == (y == 'a')
.