Skip to content

Instantly share code, notes, and snippets.

@shigemk2
Created February 4, 2015 12:05
Show Gist options
  • Select an option

  • Save shigemk2/3be4d574fb0e544b20bc to your computer and use it in GitHub Desktop.

Select an option

Save shigemk2/3be4d574fb0e544b20bc to your computer and use it in GitHub Desktop.
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