Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Task: take a list and return a tuple with a number of items
- -- satisfying a predicate and a list of the items.
- -- a verbose solution
- stats :: (a -> Bool) -> [a] -> (Int, [a])
- stats f xs = (count 0 f xs, filter f xs)
- where
- count a _ [] = a
- count a f (x:xs)
- | f x = count (a + 1) f xs
- | otherwise = count a f xs
- -- a terse solution
- stats :: (a -> Bool) -> [a] -> (Int, [a])
- stats f xs = (length sl, sl)
- where
- sl = filter f xs
- -- stats (> 5) [0..9] → (4,[6,7,8,9])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement