Functional programming recursion schemes
lazzaro.com.ar
lazzaro.com.ar
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. prod (x:xs) = x * (sum xs) prod (x:xs) = x * (self xs) prod' self [] = 1
prod' self (x:xs) = x * self xs
prod = fix prod' recr base combine (x:xs) = f x xs (recr base combine xs)This feels like it has some examples of how to use it and how NOT to use it, but there is some confusion expressed by the writer as well.
https://github.com/quchen/articles/blob/master/useful_techni...