Last active
October 30, 2015 11:56
-
-
Save davidallsopp/9119d1b8d1b040a1dd6f to your computer and use it in GitHub Desktop.
Illustrating folds - from "Haskell Programming" (http://haskellbook.com/), who in turn borrowed the idea from from Cale Gibbard from the haskell Freenode IRC channel and on the Haskell.org wiki https://wiki.haskell.org/Fold#Examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Prelude> let xs = map show [1..5] | |
Prelude> foldr (\x y -> concat ["(",x,"+",y,")"]) "0" xs | |
"(1+(2+(3+(4+(5+0)))))" | |
Prelude> let f = (\x y -> concat ["(",x,"+",y,")"]) | |
Prelude> foldl f "0" (map show [1..5]) | |
"(((((0+1)+2)+3)+4)+5)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment