Skip to content

Instantly share code, notes, and snippets.

@jbpotonnier
Created June 4, 2012 21:46
Show Gist options
  • Save jbpotonnier/2871039 to your computer and use it in GitHub Desktop.
Save jbpotonnier/2871039 to your computer and use it in GitHub Desktop.
map is just fold, or is it the other way around :P Is fold enough to have map, filter, and recursion?
foldlmap :: (a -> b) -> [a] -> [b]
foldlmap f = foldl (\ a b -> a ++ [f b]) []
foldrmap :: (a -> b) -> [a] -> [b]
foldrmap f = foldr (\ a b -> (f a) : b) []
main :: IO ()
main = do
print $ foldlmap (*2) [0 .. 5]
print $ foldrmap (*2) [0 .. 5]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment