Skip to content

Instantly share code, notes, and snippets.

@onlyshk
Created November 4, 2011 07:13
Show Gist options
  • Save onlyshk/1338838 to your computer and use it in GitHub Desktop.
Save onlyshk/1338838 to your computer and use it in GitHub Desktop.
foldl and foldr by step
-- Computing steps:
-- 1) 1 + foldr ( (+) 0 [2,3,4,5])
-- 2) 1 + 2 + foldr ( (+) 0 [3,4,5])
-- 3) 1 + 2 + 3 + foldr ( (+) 0 [4,5])
-- 4) 1 + 2 + 3 + 4 + foldr ((+) 0 [5])
-- 5) 1 + 2 + 3 + 4 + 5 + foldr ((+) 0 [])
-- 6) 1 + 2 + 3 + 4 + 5 + 0
foldr (+) 0 [1,2,3,4,5]
-- Result:
15
-- Computing steps:
-- 1) foldl (1 * 1) [2,3,4,5]
-- 2) foldl (1 * 1 * 2) [3,4,5]
-- 3) foldl (1 * 1 * 2 * 3) [4,5]
-- 4) foldl (1 * 1 * 2 * 3 * 4) [5]
-- 5) foldl (1 * 1 * 2 * 3 * 4 * 5) []
-- 6) (1 * 1 * 2 * 3 * 4 * 5)
foldl (*) 1 [1,2,3,4,5]
-- Result:
120
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment