Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Prelude hiding (map, filter, all, any, takeWhile, dropWhile)
- map :: (a -> b) -> [a] -> [b]
- -- List comprehension
- map f xs = [f x | x <- xs]
- filter :: (a -> Bool) -> [a] -> [a]
- -- Explicit recursion
- filter p [] = []
- filter p (x : xs)
- | p x = (x : filter p xs)
- | otherwise = filter p xs
- all :: (a -> Bool) -> [a] -> Bool
- -- Using the library function foldr
- all p xs = foldr (\x acc -> (p x) && acc) True xs
- -- Using recursion
- all p [] = True
- all p (x : xs)
- | p x = all p xs
- | otherwise = False
- any :: (a -> Bool) -> [a] -> Bool
- -- Using the library function foldr
- any p xs = foldr (\x acc -> (p x) || acc) False xs
- -- TODO: Using explicit recursion
- any p [] = False -- At least (>=) 1 element must be `p x`
- any p (x : xs)
- | p x = True
- | otherwise = any p xs
- -- TODO: Using list comprehension
- takeWhile :: (a -> Bool) -> [a] -> [a]
- -- Explicit recursion
- takeWhile p [] = []
- takeWhile p (x : xs)
- | p x = x : takeWhile p xs
- | otherwise = []
- dropWhile :: (a -> Bool) -> [a] -> [a]
- -- Explicit recursion
- dropWhile p [] = []
- dropWhile p (x : xs)
- | p x = dropWhile p xs -- drop
- | otherwise = (x : xs)
- -- TODO: Using foldr
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement