SlideShare uma empresa Scribd logo
1 de 1
Baixar para ler offline
∶
/ 
𝑥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

Mais conteúdo relacionado

Mais de Philip Schwarz

N-Queens Combinatorial Puzzle meets Cats
N-Queens Combinatorial Puzzle meets CatsN-Queens Combinatorial Puzzle meets Cats
N-Queens Combinatorial Puzzle meets Cats
Philip Schwarz
 

Mais de Philip Schwarz (20)

A sighting of traverse_ function in Practical FP in Scala
A sighting of traverse_ function in Practical FP in ScalaA sighting of traverse_ function in Practical FP in Scala
A sighting of traverse_ function in Practical FP in Scala
 
A sighting of traverseFilter and foldMap in Practical FP in Scala
A sighting of traverseFilter and foldMap in Practical FP in ScalaA sighting of traverseFilter and foldMap in Practical FP in Scala
A sighting of traverseFilter and foldMap in Practical FP in Scala
 
A sighting of sequence function in Practical FP in Scala
A sighting of sequence function in Practical FP in ScalaA sighting of sequence function in Practical FP in Scala
A sighting of sequence function in Practical FP in Scala
 
N-Queens Combinatorial Puzzle meets Cats
N-Queens Combinatorial Puzzle meets CatsN-Queens Combinatorial Puzzle meets Cats
N-Queens Combinatorial Puzzle meets Cats
 
Kleisli composition, flatMap, join, map, unit - implementation and interrelat...
Kleisli composition, flatMap, join, map, unit - implementation and interrelat...Kleisli composition, flatMap, join, map, unit - implementation and interrelat...
Kleisli composition, flatMap, join, map, unit - implementation and interrelat...
 
The aggregate function - from sequential and parallel folds to parallel aggre...
The aggregate function - from sequential and parallel folds to parallel aggre...The aggregate function - from sequential and parallel folds to parallel aggre...
The aggregate function - from sequential and parallel folds to parallel aggre...
 
Nat, List and Option Monoids - from scratch - Combining and Folding - an example
Nat, List and Option Monoids -from scratch -Combining and Folding -an exampleNat, List and Option Monoids -from scratch -Combining and Folding -an example
Nat, List and Option Monoids - from scratch - Combining and Folding - an example
 
Nat, List and Option Monoids - from scratch - Combining and Folding - an example
Nat, List and Option Monoids -from scratch -Combining and Folding -an exampleNat, List and Option Monoids -from scratch -Combining and Folding -an example
Nat, List and Option Monoids - from scratch - Combining and Folding - an example
 
The Sieve of Eratosthenes - Part II - Genuine versus Unfaithful Sieve - Haske...
The Sieve of Eratosthenes - Part II - Genuine versus Unfaithful Sieve - Haske...The Sieve of Eratosthenes - Part II - Genuine versus Unfaithful Sieve - Haske...
The Sieve of Eratosthenes - Part II - Genuine versus Unfaithful Sieve - Haske...
 
Sum and Product Types - The Fruit Salad & Fruit Snack Example - From F# to Ha...
Sum and Product Types -The Fruit Salad & Fruit Snack Example - From F# to Ha...Sum and Product Types -The Fruit Salad & Fruit Snack Example - From F# to Ha...
Sum and Product Types - The Fruit Salad & Fruit Snack Example - From F# to Ha...
 
Algebraic Data Types for Data Oriented Programming - From Haskell and Scala t...
Algebraic Data Types forData Oriented Programming - From Haskell and Scala t...Algebraic Data Types forData Oriented Programming - From Haskell and Scala t...
Algebraic Data Types for Data Oriented Programming - From Haskell and Scala t...
 
Jordan Peterson - The pursuit of meaning and related ethical axioms
Jordan Peterson - The pursuit of meaning and related ethical axiomsJordan Peterson - The pursuit of meaning and related ethical axioms
Jordan Peterson - The pursuit of meaning and related ethical axioms
 
Defining filter using (a) recursion (b) folding (c) folding with S, B and I c...
Defining filter using (a) recursion (b) folding (c) folding with S, B and I c...Defining filter using (a) recursion (b) folding (c) folding with S, B and I c...
Defining filter using (a) recursion (b) folding (c) folding with S, B and I c...
 
Defining filter using (a) recursion (b) folding with S, B and I combinators (...
Defining filter using (a) recursion (b) folding with S, B and I combinators (...Defining filter using (a) recursion (b) folding with S, B and I combinators (...
Defining filter using (a) recursion (b) folding with S, B and I combinators (...
 
The Sieve of Eratosthenes - Part 1 - with minor corrections
The Sieve of Eratosthenes - Part 1 - with minor correctionsThe Sieve of Eratosthenes - Part 1 - with minor corrections
The Sieve of Eratosthenes - Part 1 - with minor corrections
 
The Sieve of Eratosthenes - Part 1
The Sieve of Eratosthenes - Part 1The Sieve of Eratosthenes - Part 1
The Sieve of Eratosthenes - Part 1
 
The Uniform Access Principle
The Uniform Access PrincipleThe Uniform Access Principle
The Uniform Access Principle
 
Computer Graphics in Java and Scala - Part 1b
Computer Graphics in Java and Scala - Part 1bComputer Graphics in Java and Scala - Part 1b
Computer Graphics in Java and Scala - Part 1b
 
The Expression Problem - Part 2
The Expression Problem - Part 2The Expression Problem - Part 2
The Expression Problem - Part 2
 
Computer Graphics in Java and Scala - Part 1
Computer Graphics in Java and Scala - Part 1Computer Graphics in Java and Scala - Part 1
Computer Graphics in Java and Scala - Part 1
 

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

  • 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