Skip to content

Instantly share code, notes, and snippets.

@davidallsopp
Last active October 30, 2015 11:56
Show Gist options
  • Save davidallsopp/9119d1b8d1b040a1dd6f to your computer and use it in GitHub Desktop.
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
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