4 ms·
I couldn't understand the authors implementation. I found this in prelude. So much simpler. dropWhile :: (a -> Bool) -> [a] -> [a] dropWhile p [
by thewarrior 12y ago
I couldn't understand the authors implementation. I found this in prelude. So much simpler.
dropWhile :: (a -> Bool) -> [a] -> [a]
dropWhile p [] = []
dropWhile p xs@(x:xs')
| p x = dropWhile p xs'
| otherwise = xs
I feel that dropWhile isn't that kind of operation fold was designed for.
EDIT:
I was somewhat wrong.
After some googling I found this clever implementation :
dropWhilePair :: (a -> Bool) -> [a] -> ([a], [a])
dropWhilePair p = foldr f v
where
f x (ys,xs) = (if p x then ys else x : xs, x : xs)
v = ([],[])
But this returns a tuple whose first element is the answer we need.