Skip to content

Instantly share code, notes, and snippets.

@akirillov
Created November 5, 2015 21:53
Show Gist options
  • Save akirillov/9b512bca7fe04b91ceba to your computer and use it in GitHub Desktop.
Save akirillov/9b512bca7fe04b91ceba to your computer and use it in GitHub Desktop.
Library functions implementation with fold (from LYAHFGG)
maximum' :: (Ord a) => [a] -> a
maximum' = foldr1 (\x acc -> if x > acc then x else acc)
reverse' :: [a] -> [a]
reverse' = foldl (\acc x -> x : acc) []
product' :: (Num a) => [a] -> a
product' = foldr1 (*)
filter' :: (a -> Bool) -> [a] -> [a]
filter' p = foldr (\x acc -> if p x then x : acc else acc) []
head' :: [a] -> a
head' = foldr1 (\x _ -> x)
last' :: [a] -> a
last' = foldl1 (\_ x -> x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment