Share
Embed
Folding Cheat Sheetย #1
Folding over recursively defined data structures for natural numbers and lists.
Keywords: folding, list, lists, nat, natural numbers, recursive datatype, recursive function, right fold.
Philip Schwarz PRO
March 29, 2024
Transcript
3 slides
-
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 ๐๐๐๐๐: