SlideShare una empresa de Scribd logo
1 de 3
Descargar para leer sin conexión
CHEAT-SHEET
Folding
#1
∶
/ 
𝒂𝟎 ∶
/ 
𝒂𝟏 ∶
/ 
𝒂𝟐 ∶
/ 
𝒂𝟑
𝒇
/ 
𝒂𝟎 𝒇
/ 
𝒂𝟏 𝒇
/ 
𝒂𝟐 𝒇
/ 
𝒂𝟑 𝒆
@philip_schwarz
slides by https://fpilluminated.com/
𝐝𝐚𝐭𝐚 𝑵𝒂𝒕 = 𝒁𝒆𝒓𝒐 | 𝑺𝒖𝒄𝒄 𝑵𝒂𝒕 𝐝𝐚𝐭𝐚 𝑳𝒊𝒔𝒕 α = 𝑵𝒊𝒍 | 𝑪𝒐𝒏𝒔 α (𝑳𝒊𝒔𝒕 α)
𝑓 :: 𝑵𝒂𝒕 → 𝛼
𝑓 𝒁𝒆𝒓𝒐 = 𝑐
𝑓 𝑺𝒖𝒄𝒄 𝑛 = ℎ 𝑓 𝑛
𝑓𝑜𝑙𝑑𝑛 ∷ 𝛼 → 𝛼 → 𝛼 → 𝑵𝒂𝒕 → 𝛼
𝑓𝑜𝑙𝑑𝑛 ℎ 𝑐 𝒁𝒆𝒓𝒐 = 𝑐
𝑓𝑜𝑙𝑑𝑛 ℎ 𝑐 𝑺𝒖𝒄𝒄 𝑛 = ℎ 𝑓𝑜𝑙𝑑𝑛 ℎ 𝑐 𝑛
𝑚 + 𝑛 = 𝑓𝑜𝑙𝑑𝑛 𝑺𝒖𝒄𝒄 𝑚 𝑛
𝑚 × 𝑛 = 𝑓𝑜𝑙𝑑𝑛 𝜆𝑥. 𝑥 + 𝑚 𝒁𝒆𝒓𝒐 𝑛
𝑚 ↑ 𝑛 = 𝑓𝑜𝑙𝑑𝑛 𝜆𝑥. 𝑥 × 𝑚 𝑺𝒖𝒄𝒄 𝒁𝒆𝒓𝒐 𝑛
+ ∷ 𝑵𝒂𝒕 → 𝑵𝒂𝒕 → 𝑵𝒂𝒕
𝑚 + 𝒁𝒆𝒓𝒐 = 𝑚
𝑚 + 𝑺𝒖𝒄𝒄 𝑛 = 𝑺𝒖𝒄𝒄 𝑚 + 𝑛
(×) ∷ 𝑵𝒂𝒕 → 𝑵𝒂𝒕 → 𝑵𝒂𝒕
𝑚 × 𝒁𝒆𝒓𝒐 = 𝒁𝒆𝒓𝒐
𝑚 × 𝑺𝒖𝒄𝒄 𝑛 = 𝑚 × 𝑛 + 𝑚
(↑) ∷ 𝑵𝒂𝒕 → 𝑵𝒂𝒕 → 𝑵𝒂𝒕
𝑚 ↑ 𝒁𝒆𝒓𝒐 = 𝑺𝒖𝒄𝒄 𝒁𝒆𝒓𝒐
𝑚 ↑ 𝑺𝒖𝒄𝒄 𝑛 = 𝑚 ↑ 𝑛 × 𝑚
𝑓𝑜𝑙𝑑𝑟 ∷ 𝛼 → 𝛽 → 𝛽 → 𝛽 → 𝛼 → 𝛽
𝑓𝑜𝑙𝑑𝑟 𝑓 𝑏 𝑵𝒊𝒍 = 𝑏
𝑓𝑜𝑙𝑑𝑟 𝑓 𝑏 (𝑪𝒐𝒏𝒔 𝑥 𝑥𝑠) = 𝑓 𝑥 𝑓𝑜𝑙𝑑𝑟 𝑓 𝑏 𝑥𝑠
𝑓 :: 𝑳𝒊𝒔𝒕 𝛼 → 𝛽
𝑓 𝑵𝒊𝒍 = 𝑐
𝑓 𝑪𝒐𝒏𝒔 𝑥 𝑥𝑠 = ℎ 𝑥 (𝑓 𝑥𝑠)
𝑠𝑢𝑚 ∷ 𝑳𝒊𝒔𝒕 𝑵𝒂𝒕 → 𝑵𝒂𝒕
𝑠𝑢𝑚 𝑵𝒊𝒍 = 𝒁𝒆𝒓𝒐
𝑠𝑢𝑚 𝑪𝒐𝒏𝒔 𝑥 𝑥𝑠 = 𝑥 + (𝑠𝑢𝑚 𝑥𝑠)
𝑎𝑝𝑝𝑒𝑛𝑑 ∷ 𝑳𝒊𝒔𝒕 𝛼 → 𝑳𝒊𝒔𝒕 𝛼 → 𝑳𝒊𝒔𝒕 𝛼
𝑎𝑝𝑝𝑒𝑛𝑑 𝑵𝒊𝒍 𝑦𝑠 = 𝑦𝑠
𝑎𝑝𝑝𝑒𝑛𝑑 𝑪𝒐𝒏𝒔 𝑥 𝑥𝑠 𝑦𝑠 = 𝑪𝒐𝒏𝒔 𝑥 (𝑎𝑝𝑝𝑒𝑛𝑑 𝑥𝑠 𝑦𝑠)
𝑙𝑒𝑛𝑔𝑡ℎ ∷ 𝑳𝒊𝒔𝒕 𝛼 → 𝑵𝒂𝒕
𝑙𝑒𝑛𝑔𝑡ℎ 𝑵𝒊𝒍 = 𝒁𝒆𝒓𝒐
𝑙𝑒𝑛𝑔𝑡ℎ 𝑪𝒐𝒏𝒔 𝑥 𝑥𝑠 = 𝑺𝒖𝒄𝒄 𝒁𝒆𝒓𝒐 + (𝑙𝑒𝑛𝑔𝑡ℎ 𝑥𝑠)
𝑠𝑢𝑚 𝑥𝑠 = 𝑓𝑜𝑙𝑑𝑟 + 𝒁𝒆𝒓𝒐 𝑥𝑠
𝑙𝑒𝑛𝑔𝑡ℎ 𝑥𝑠 = 𝑓𝑜𝑙𝑑𝑟 𝜆𝑥. 𝜆𝑛. 𝑛 + 𝑺𝒖𝒄𝒄 𝒁𝒆𝒓𝒐 𝒁𝒆𝒓𝒐 𝑥𝑠
𝑎𝑝𝑝𝑒𝑛𝑑 𝑥𝑠 𝑦𝑠 = 𝑓𝑜𝑙𝑑𝑟 𝑪𝒐𝒏𝒔 𝑦𝑠 𝑥𝑠
Common pattern for many recursive functions over 𝑵𝒂𝒕 : Common pattern for many recursive functions over 𝑳𝒊𝒔𝒕:
𝑐 :: 𝛼
ℎ :: 𝛼 → 𝛼
𝑐 :: 𝛽
ℎ :: 𝛼 → 𝛽
Three examples of such functions: Three examples of such functions:
The common pattern can be captured in a function: The common pattern can be captured in a function:
The three sample functions implemented using 𝑓𝑜𝑙𝑑𝑛: The three sample functions implemented using 𝑓𝑜𝑙𝑑𝑟:
https://fpilluminated.com/
inspired
by

Más contenido relacionado

Similar a Folding Cheat Sheet #1 - first in a series

Similar a Folding Cheat Sheet #1 - first in a series (20)

Folding Cheat Sheet #2 - second in a series
Folding Cheat Sheet #2 - second in a seriesFolding Cheat Sheet #2 - second in a series
Folding Cheat Sheet #2 - second in a series
 
Folding Cheat Sheet #2 - second in a series
Folding Cheat Sheet #2 - second in a seriesFolding Cheat Sheet #2 - second in a series
Folding Cheat Sheet #2 - second in a series
 
Sets
SetsSets
Sets
 
Photosynthesis
PhotosynthesisPhotosynthesis
Photosynthesis
 
Instrumental Variables
Instrumental VariablesInstrumental Variables
Instrumental Variables
 
SUEC 高中 Adv Maths (Irrational Part 3)
SUEC 高中 Adv Maths (Irrational Part 3)SUEC 高中 Adv Maths (Irrational Part 3)
SUEC 高中 Adv Maths (Irrational Part 3)
 
Review of generative adversarial nets
Review of generative adversarial netsReview of generative adversarial nets
Review of generative adversarial nets
 
07.mdsd_modelado_termicos_liquidos
07.mdsd_modelado_termicos_liquidos07.mdsd_modelado_termicos_liquidos
07.mdsd_modelado_termicos_liquidos
 
On ranges and null spaces of a special type of operator named 𝝀 − 𝒋𝒆𝒄𝒕𝒊𝒐𝒏. – ...
On ranges and null spaces of a special type of operator named 𝝀 − 𝒋𝒆𝒄𝒕𝒊𝒐𝒏. – ...On ranges and null spaces of a special type of operator named 𝝀 − 𝒋𝒆𝒄𝒕𝒊𝒐𝒏. – ...
On ranges and null spaces of a special type of operator named 𝝀 − 𝒋𝒆𝒄𝒕𝒊𝒐𝒏. – ...
 
Enzymes
EnzymesEnzymes
Enzymes
 
SUEC 高中 Adv Maths (Biquadratic Equation, Method of Changing the Variable, Rec...
SUEC 高中 Adv Maths (Biquadratic Equation, Method of Changing the Variable, Rec...SUEC 高中 Adv Maths (Biquadratic Equation, Method of Changing the Variable, Rec...
SUEC 高中 Adv Maths (Biquadratic Equation, Method of Changing the Variable, Rec...
 
Ch 5 integration
Ch 5 integration  Ch 5 integration
Ch 5 integration
 
Integration
IntegrationIntegration
Integration
 
SUEC 高中 Adv Maths (Quadratic Equation in One Variable)
SUEC 高中 Adv Maths (Quadratic Equation in One Variable)SUEC 高中 Adv Maths (Quadratic Equation in One Variable)
SUEC 高中 Adv Maths (Quadratic Equation in One Variable)
 
Carbohydrate Metabolism
Carbohydrate MetabolismCarbohydrate Metabolism
Carbohydrate Metabolism
 
Blow up in a degenerate keller--segel system(Eng.)
Blow up in a degenerate keller--segel system(Eng.)Blow up in a degenerate keller--segel system(Eng.)
Blow up in a degenerate keller--segel system(Eng.)
 
2018 Geometri Transformasi Perkalian 5 Isometri Kelompok 8 Rombel 3
2018 Geometri Transformasi Perkalian 5 Isometri Kelompok 8 Rombel 32018 Geometri Transformasi Perkalian 5 Isometri Kelompok 8 Rombel 3
2018 Geometri Transformasi Perkalian 5 Isometri Kelompok 8 Rombel 3
 
05.mdsd_modelado_mecanico_electrico
05.mdsd_modelado_mecanico_electrico05.mdsd_modelado_mecanico_electrico
05.mdsd_modelado_mecanico_electrico
 
Teoria Numérica (Palestra 01)
Teoria Numérica (Palestra 01)Teoria Numérica (Palestra 01)
Teoria Numérica (Palestra 01)
 
Selection on Observables
Selection on ObservablesSelection on Observables
Selection on Observables
 

Más 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
 

Más de Philip Schwarz (20)

Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
Scala Left Fold Parallelisation - Three Approaches
Scala Left Fold Parallelisation- Three ApproachesScala Left Fold Parallelisation- Three Approaches
Scala Left Fold Parallelisation - Three Approaches
 
Tagless Final Encoding - Algebras and Interpreters and also Programs
Tagless Final Encoding - Algebras and Interpreters and also ProgramsTagless Final Encoding - Algebras and Interpreters and also Programs
Tagless Final Encoding - Algebras and Interpreters and also Programs
 
Fusing Transformations of Strict Scala Collections with Views
Fusing Transformations of Strict Scala Collections with ViewsFusing Transformations of Strict Scala Collections with Views
Fusing Transformations of Strict Scala Collections with Views
 
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
 

Último

Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
drm1699
 
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Lisi Hocke
 

Último (20)

Microsoft365_Dev_Security_2024_05_16.pdf
Microsoft365_Dev_Security_2024_05_16.pdfMicrosoft365_Dev_Security_2024_05_16.pdf
Microsoft365_Dev_Security_2024_05_16.pdf
 
CERVED e Neo4j su una nuvola, migrazione ed evoluzione di un grafo mission cr...
CERVED e Neo4j su una nuvola, migrazione ed evoluzione di un grafo mission cr...CERVED e Neo4j su una nuvola, migrazione ed evoluzione di un grafo mission cr...
CERVED e Neo4j su una nuvola, migrazione ed evoluzione di un grafo mission cr...
 
Automate your OpenSIPS config tests - OpenSIPS Summit 2024
Automate your OpenSIPS config tests - OpenSIPS Summit 2024Automate your OpenSIPS config tests - OpenSIPS Summit 2024
Automate your OpenSIPS config tests - OpenSIPS Summit 2024
 
UNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale Ibrida
UNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale IbridaUNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale Ibrida
UNI DI NAPOLI FEDERICO II - Il ruolo dei grafi nell'AI Conversazionale Ibrida
 
GraphSummit Milan - Visione e roadmap del prodotto Neo4j
GraphSummit Milan - Visione e roadmap del prodotto Neo4jGraphSummit Milan - Visione e roadmap del prodotto Neo4j
GraphSummit Milan - Visione e roadmap del prodotto Neo4j
 
Test Automation Design Patterns_ A Comprehensive Guide.pdf
Test Automation Design Patterns_ A Comprehensive Guide.pdfTest Automation Design Patterns_ A Comprehensive Guide.pdf
Test Automation Design Patterns_ A Comprehensive Guide.pdf
 
Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...
Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...
Abortion Clinic In Johannesburg ](+27832195400*)[ 🏥 Safe Abortion Pills in Jo...
 
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
 
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
Wired_2.0_CREATE YOUR ULTIMATE LEARNING ENVIRONMENT_JCON_16052024
 
Navigation in flutter – how to add stack, tab, and drawer navigators to your ...
Navigation in flutter – how to add stack, tab, and drawer navigators to your ...Navigation in flutter – how to add stack, tab, and drawer navigators to your ...
Navigation in flutter – how to add stack, tab, and drawer navigators to your ...
 
The Strategic Impact of Buying vs Building in Test Automation
The Strategic Impact of Buying vs Building in Test AutomationThe Strategic Impact of Buying vs Building in Test Automation
The Strategic Impact of Buying vs Building in Test Automation
 
BusinessGPT - Security and Governance for Generative AI
BusinessGPT  - Security and Governance for Generative AIBusinessGPT  - Security and Governance for Generative AI
BusinessGPT - Security and Governance for Generative AI
 
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
 
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
Abortion Pills For Sale WhatsApp[[+27737758557]] In Birch Acres, Abortion Pil...
 
Software Engineering - Introduction + Process Models + Requirements Engineering
Software Engineering - Introduction + Process Models + Requirements EngineeringSoftware Engineering - Introduction + Process Models + Requirements Engineering
Software Engineering - Introduction + Process Models + Requirements Engineering
 
Auto Affiliate AI Earns First Commission in 3 Hours..pdf
Auto Affiliate  AI Earns First Commission in 3 Hours..pdfAuto Affiliate  AI Earns First Commission in 3 Hours..pdf
Auto Affiliate AI Earns First Commission in 3 Hours..pdf
 
Weeding your micro service landscape.pdf
Weeding your micro service landscape.pdfWeeding your micro service landscape.pdf
Weeding your micro service landscape.pdf
 
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
Team Transformation Tactics for Holistic Testing and Quality (NewCrafts Paris...
 
From Theory to Practice: Utilizing SpiraPlan's REST API
From Theory to Practice: Utilizing SpiraPlan's REST APIFrom Theory to Practice: Utilizing SpiraPlan's REST API
From Theory to Practice: Utilizing SpiraPlan's REST API
 
The mythical technical debt. (Brooke, please, forgive me)
The mythical technical debt. (Brooke, please, forgive me)The mythical technical debt. (Brooke, please, forgive me)
The mythical technical debt. (Brooke, please, forgive me)
 

Folding Cheat Sheet #1 - first in a series

  • 1. CHEAT-SHEET Folding #1 ∶ / 𝒂𝟎 ∶ / 𝒂𝟏 ∶ / 𝒂𝟐 ∶ / 𝒂𝟑 𝒇 / 𝒂𝟎 𝒇 / 𝒂𝟏 𝒇 / 𝒂𝟐 𝒇 / 𝒂𝟑 𝒆 @philip_schwarz slides by https://fpilluminated.com/
  • 2. 𝐝𝐚𝐭𝐚 𝑵𝒂𝒕 = 𝒁𝒆𝒓𝒐 | 𝑺𝒖𝒄𝒄 𝑵𝒂𝒕 𝐝𝐚𝐭𝐚 𝑳𝒊𝒔𝒕 α = 𝑵𝒊𝒍 | 𝑪𝒐𝒏𝒔 α (𝑳𝒊𝒔𝒕 α) 𝑓 :: 𝑵𝒂𝒕 → 𝛼 𝑓 𝒁𝒆𝒓𝒐 = 𝑐 𝑓 𝑺𝒖𝒄𝒄 𝑛 = ℎ 𝑓 𝑛 𝑓𝑜𝑙𝑑𝑛 ∷ 𝛼 → 𝛼 → 𝛼 → 𝑵𝒂𝒕 → 𝛼 𝑓𝑜𝑙𝑑𝑛 ℎ 𝑐 𝒁𝒆𝒓𝒐 = 𝑐 𝑓𝑜𝑙𝑑𝑛 ℎ 𝑐 𝑺𝒖𝒄𝒄 𝑛 = ℎ 𝑓𝑜𝑙𝑑𝑛 ℎ 𝑐 𝑛 𝑚 + 𝑛 = 𝑓𝑜𝑙𝑑𝑛 𝑺𝒖𝒄𝒄 𝑚 𝑛 𝑚 × 𝑛 = 𝑓𝑜𝑙𝑑𝑛 𝜆𝑥. 𝑥 + 𝑚 𝒁𝒆𝒓𝒐 𝑛 𝑚 ↑ 𝑛 = 𝑓𝑜𝑙𝑑𝑛 𝜆𝑥. 𝑥 × 𝑚 𝑺𝒖𝒄𝒄 𝒁𝒆𝒓𝒐 𝑛 + ∷ 𝑵𝒂𝒕 → 𝑵𝒂𝒕 → 𝑵𝒂𝒕 𝑚 + 𝒁𝒆𝒓𝒐 = 𝑚 𝑚 + 𝑺𝒖𝒄𝒄 𝑛 = 𝑺𝒖𝒄𝒄 𝑚 + 𝑛 (×) ∷ 𝑵𝒂𝒕 → 𝑵𝒂𝒕 → 𝑵𝒂𝒕 𝑚 × 𝒁𝒆𝒓𝒐 = 𝒁𝒆𝒓𝒐 𝑚 × 𝑺𝒖𝒄𝒄 𝑛 = 𝑚 × 𝑛 + 𝑚 (↑) ∷ 𝑵𝒂𝒕 → 𝑵𝒂𝒕 → 𝑵𝒂𝒕 𝑚 ↑ 𝒁𝒆𝒓𝒐 = 𝑺𝒖𝒄𝒄 𝒁𝒆𝒓𝒐 𝑚 ↑ 𝑺𝒖𝒄𝒄 𝑛 = 𝑚 ↑ 𝑛 × 𝑚 𝑓𝑜𝑙𝑑𝑟 ∷ 𝛼 → 𝛽 → 𝛽 → 𝛽 → 𝛼 → 𝛽 𝑓𝑜𝑙𝑑𝑟 𝑓 𝑏 𝑵𝒊𝒍 = 𝑏 𝑓𝑜𝑙𝑑𝑟 𝑓 𝑏 (𝑪𝒐𝒏𝒔 𝑥 𝑥𝑠) = 𝑓 𝑥 𝑓𝑜𝑙𝑑𝑟 𝑓 𝑏 𝑥𝑠 𝑓 :: 𝑳𝒊𝒔𝒕 𝛼 → 𝛽 𝑓 𝑵𝒊𝒍 = 𝑐 𝑓 𝑪𝒐𝒏𝒔 𝑥 𝑥𝑠 = ℎ 𝑥 (𝑓 𝑥𝑠) 𝑠𝑢𝑚 ∷ 𝑳𝒊𝒔𝒕 𝑵𝒂𝒕 → 𝑵𝒂𝒕 𝑠𝑢𝑚 𝑵𝒊𝒍 = 𝒁𝒆𝒓𝒐 𝑠𝑢𝑚 𝑪𝒐𝒏𝒔 𝑥 𝑥𝑠 = 𝑥 + (𝑠𝑢𝑚 𝑥𝑠) 𝑎𝑝𝑝𝑒𝑛𝑑 ∷ 𝑳𝒊𝒔𝒕 𝛼 → 𝑳𝒊𝒔𝒕 𝛼 → 𝑳𝒊𝒔𝒕 𝛼 𝑎𝑝𝑝𝑒𝑛𝑑 𝑵𝒊𝒍 𝑦𝑠 = 𝑦𝑠 𝑎𝑝𝑝𝑒𝑛𝑑 𝑪𝒐𝒏𝒔 𝑥 𝑥𝑠 𝑦𝑠 = 𝑪𝒐𝒏𝒔 𝑥 (𝑎𝑝𝑝𝑒𝑛𝑑 𝑥𝑠 𝑦𝑠) 𝑙𝑒𝑛𝑔𝑡ℎ ∷ 𝑳𝒊𝒔𝒕 𝛼 → 𝑵𝒂𝒕 𝑙𝑒𝑛𝑔𝑡ℎ 𝑵𝒊𝒍 = 𝒁𝒆𝒓𝒐 𝑙𝑒𝑛𝑔𝑡ℎ 𝑪𝒐𝒏𝒔 𝑥 𝑥𝑠 = 𝑺𝒖𝒄𝒄 𝒁𝒆𝒓𝒐 + (𝑙𝑒𝑛𝑔𝑡ℎ 𝑥𝑠) 𝑠𝑢𝑚 𝑥𝑠 = 𝑓𝑜𝑙𝑑𝑟 + 𝒁𝒆𝒓𝒐 𝑥𝑠 𝑙𝑒𝑛𝑔𝑡ℎ 𝑥𝑠 = 𝑓𝑜𝑙𝑑𝑟 𝜆𝑥. 𝜆𝑛. 𝑛 + 𝑺𝒖𝒄𝒄 𝒁𝒆𝒓𝒐 𝒁𝒆𝒓𝒐 𝑥𝑠 𝑎𝑝𝑝𝑒𝑛𝑑 𝑥𝑠 𝑦𝑠 = 𝑓𝑜𝑙𝑑𝑟 𝑪𝒐𝒏𝒔 𝑦𝑠 𝑥𝑠 Common pattern for many recursive functions over 𝑵𝒂𝒕 : Common pattern for many recursive functions over 𝑳𝒊𝒔𝒕: 𝑐 :: 𝛼 ℎ :: 𝛼 → 𝛼 𝑐 :: 𝛽 ℎ :: 𝛼 → 𝛽 Three examples of such functions: Three examples of such functions: The common pattern can be captured in a function: The common pattern can be captured in a function: The three sample functions implemented using 𝑓𝑜𝑙𝑑𝑛: The three sample functions implemented using 𝑓𝑜𝑙𝑑𝑟: