Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import Prelude hiding (takeWhile)
sum :: Num a => [a] -> a
sum = foldr (+) 0
product :: Num a => [a] -> a
product = foldr (*) 1
and :: [Bool] -> Bool
and = foldr (&&) True
-- NB pointful function definitions compile just fine without type annotations
and' xs = foldr (&&) True xs
-- length [] = 0
-- length xs = foldr (x:xs) n = foldr xs (n + 1)
length' :: [a] -> Int
length' = foldr (\_ n -> 1+n) 0
length'' xs = foldr (\_ n -> 1+n) 0 xs
--reverse (x:xs) = reverse xs ++ [x]
--reverse [] = []
reverse :: [a] -> [a]
reverse = foldr (\x xs -> xs ++ [x]) []
takeWhile :: (a -> Bool) -> [a] -> [a]
takeWhile p [] = []
takeWhile p (x:xs)
| p x = x : takeWhile p xs
| otherwise = []
dropWhile p (x:xs)
| p x = dropWhile p xs
| otherwise = xs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.