([]:y)
makes no sense, since it should be a list of lists of items, but y
is a list of items. Even if the type system would not complain (for example because of a dynamically typed language like Python), it would not make much sense, since you drop ys
, which would thus omit the rest of the recursion.
Another problem is that you write (x:xs)
as first parameter, which means that the items in the list you fold are lists themselves, but given I understand it correctly, you fold a String
, so the element is simple character:
You can fix this by returning []:y:ys
:
split :: (Foldable f, Eq a) => a -> f a -> [[a]]
split sep str = foldr op [[]] str
where op x ~(y:ys)
| x == sep = []:y:ys
| otherwise = (x:y):ys
For example:
Prelude> split '-' "this-is-kebab-case"
["this","is","kebab","case"]