Embed presentation
Download as PDF, PPTX

![∶
/
𝑥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](https://crownmelresort.com/image.slidesharecdn.com/2024-06-23-folding-cheat-sheet-5-240623100613-a1fd837e/75/Folding-Cheat-Sheet-5-fifth-in-a-series-2-2048.jpg)

The document focuses on folding functions in programming, specifically demonstrating the concepts of foldr and foldl using examples and syntactic structures. It explains how these folding techniques can result in identity and reverse functions when applied to lists. Key elements include the definition of operations and the processing of lists through functional programming methods.

![∶
/
𝑥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](https://crownmelresort.com/image.slidesharecdn.com/2024-06-23-folding-cheat-sheet-5-240623100613-a1fd837e/75/Folding-Cheat-Sheet-5-fifth-in-a-series-2-2048.jpg)