Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Folding a list right and left using cons and ni...

Folding a list right and left using cons and nil results in the identity and reverse functions

single slide - folding a list right and left using cons and nil results in the identity and reverse functions - DOWNLOAD for original quality

keywords: cons, fold, fold left, fold right, folding, foldl, foldleft, foldr, foldright, functional programming, haskell, identity, left fold, list, nil, recursion, reverse, right fold, tail recursion

Philip Schwarz

September 20, 2020
Tweet

More Decks by Philip Schwarz

Other Decks in Programming

Transcript

  1. ∶ / \ 1 ∶ / \ 2 ∶ /

    \ 3 ∶ / \ 4 ‥ / \ ‥ 4 / \ ‥ 3 / \ ‥ 2 / \ [ ] 1 ∶ / \ 4 ∶ / \ 3 ∶ / \ 2 ∶ / \ 1 4 ∶ (3 ∶ 2 ∶ 1 ∶ ) ∶ / \ 1 ∶ / \ 2 ∶ / \ 3 ∶ / \ 4 1 : (2 : 3 : 4 : ) (( ‥1 ‥2 )‥3 )‥4 var acc = [ ] foreach(x in xs) acc = acc ‥x) return acc ∶ ‥ = [1 , 2 , 3 , 4 ] ∶ = = λ. λ. ∶ ‥ = = λ. λ. ∶ ∷ → → → → → = : = ∷ → → → → → = : = ∶ = λ. λ. ∶ ‥ = λ. λ. ∶ ‥ = ∶ ℎ = ∶ / \ ℎ . . / \ ℎ equivalent to [4 , 3 , 2 , 1 ] [1 , 2 , 3 , 4 ] > id = foldr (:) [] > rev = foldl (flip (:)) [] > id [1,2,3,4] [1,2,3,4] > rev [1,2,3,4] [4,3,2,1] > id = foldr (\x y -> x:y) [] > rev = foldl (\x y -> y:x) [] > id [1,2,3,4] [1,2,3,4] > rev [1,2,3,4] [4,3,2,1] : ∶ ℎ ∶ ℎ : ∶ ℎ ℎ var acc = e foreach(x in xs) acc = f (acc, x) return acc folding a list right and left using Cons and Nil results in the identity and reverse functions