Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
reverse' :: [a] -> [a]
reverse' = foldl (\acc x -> x : acc) []
reverse'' :: [a] -> [a]
reverse'' = foldl (flip (:)) []
product' :: (Num a) => [a] -> a
product' = foldl (*) 1
filter' :: (a -> Bool) -> [a] -> [a]
filter' p = foldr (\x acc -> if p x then x : acc else acc) []
last' :: [a] -> a
last' = foldl1 (\_ x -> x)
main = do
print $ reverse' [1,2,3,4,5]
print $ reverse'' [1,2,3,4,5]
print $ product' [1,2,3,4,5]
print $ filter' (< 3) [1,2,3,4,5]
print $ last' [1,2,3,4,5]
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.