Share
Embed
Folding Cheat Sheetย #2
Programmatic and Mathematical definitions of right fold and left fold.
Keywords: folding, list, left fold, right fold, mathematical definition, programmatic definition
Philip Schwarz PRO
March 30, 2024
Transcript
4 slides
-
CHEAT-SHEET Folding #2 โถ / \ ๐๐ โถ / \
๐๐ โถ / \ ๐๐ โถ / \ ๐๐ ๐ / \ ๐๐ ๐ / \ ๐๐ ๐ / \ ๐๐ ๐ / \ ๐๐ ๐ @philip_schwarz slides by https://fpilluminated.com/ -
โถ / \ ๐0 โถ / \ ๐1 โถ /
\ ๐2 โถ / \ ๐3 ๐ / \ ๐ ๐3 / \ ๐ ๐2 / \ ๐ ๐1 / \ ๐ ๐0 ๐ / \ ๐0 ๐ / \ ๐1 ๐ / \ ๐2 ๐ / \ ๐3 ๐ ๐0 : (๐1 : ๐2 : ๐3 : ) ๐ ๐ ๐ ๐ ๐ ๐0 ๐1 ๐2 ๐3 var ๐๐๐ = ๐ foreach(๐ in ๐๐ ) ๐๐๐ = ๐(๐๐๐, ๐) return ๐๐๐ ๐๐๐๐๐ ๐ ๐ ๐๐ ๐๐๐๐๐ ๐ ๐ ๐๐ ๐๐ = [๐0 , ๐1 , ๐2 , ๐3 ] ๐๐๐๐๐ โท ๐ผ โ ๐ฝ โ ๐ฝ โ ๐ฝ โ ๐ผ โ ๐ฝ ๐๐๐๐๐ ๐ ๐ = ๐ ๐๐๐๐๐ ๐ ๐ ๐ฅ: ๐ฅ๐ = ๐ ๐ฅ ๐๐๐๐๐ ๐ ๐ ๐ฅ๐ ๐๐๐๐๐ โท ๐ฝ โ ๐ผ โ ๐ฝ โ ๐ฝ โ ๐ผ โ ๐ฝ ๐๐๐๐๐ ๐ ๐ = ๐ ๐๐๐๐๐ ๐ ๐ ๐ฅ: ๐ฅ๐ = ๐๐๐๐๐ ๐ ๐ ๐ ๐ฅ ๐ฅ๐ ๐๐๐๐๐๐๐: โถ ๐ค๐๐กโ ๐ ๐ค๐๐กโ ๐ ๐๐๐๐๐ ๐ ๐ [๐0 , ๐1 , ๐2 , ๐3 ] ๐ ๐0 ๐๐๐๐๐ ๐ ๐ [๐1 , ๐2 , ๐3 ] ๐ ๐0 (๐ ๐1 (๐๐๐๐๐ ๐ ๐ [๐2 , ๐3 ])) ๐ ๐0 (๐ ๐1 (๐ ๐2 (๐๐๐๐๐ ๐ ๐ [๐3 ]))) ๐ ๐0 (๐ ๐1 (๐ ๐2 (๐ ๐3 (๐๐๐๐๐ ๐ ๐ [ ])))) ๐ ๐0 (๐ ๐1 (๐ ๐2 (๐ ๐3 ๐))) ๐ ๐0 (๐ ๐1 (๐ ๐2 (๐ ๐3 ๐))) ๐๐๐๐๐ ๐ ๐ [๐0 , ๐1 , ๐2 , ๐3 ] ๐๐๐๐๐ ๐ ๐ ๐ ๐0 [๐1 , ๐2 , ๐3 ] ๐๐๐๐๐ ๐ ๐ ๐ ๐ ๐0 ๐1 [๐2 , ๐3 ] ๐๐๐๐๐ ๐ ๐ ๐ ๐ ๐ ๐0 ๐1 ๐2 [๐3 ] ๐๐๐๐๐ ๐ ๐ ๐ ๐ ๐ ๐ ๐0 ๐1 ๐2 ๐3 [ ] ๐ ๐ ๐ ๐ ๐ ๐0 ๐1 ๐2 ๐3 Programmatic definition of right fold and left fold ๐๐๐๐๐ associates ๐ from the right ๐๐๐๐๐ associates ๐ from the left -
โถ / \ ๐0 โถ / \ ๐1 โถ /
\ ๐2 โถ / \ ๐3 ๐ / \ ๐ ๐3 / \ ๐ ๐2 / \ ๐ ๐1 / \ ๐ ๐0 ๐ / \ ๐0 ๐ / \ ๐1 ๐ / \ ๐2 ๐ / \ ๐3 ๐ ๐0 : (๐1 : ๐2 : ๐3 : ) ๐(๐ ๐ ๐ ๐, ๐0 , ๐1 , ๐2 , ๐3 ) ๐๐๐๐๐ ๐๐ ๐๐๐๐๐ ๐๐ ๐๐ = [๐0 , ๐1 , ๐2 , ๐3 ] ๐(๐0 , ๐(๐1 , ๐(๐2 , ๐(๐3 , ๐)))) ๐๐๐๐๐ = ๐; ๐๐๐๐๐ ๐ฅ๐ โงบ [๐ฅ] = ๐(๐๐๐๐๐ ๐ฅ๐ , ๐ฅ) ๐๐๐๐๐ = ๐; ๐๐๐๐๐ ๐ฅ โงบ ๐ฅ๐ = ๐(๐ฅ, ๐๐๐๐๐(๐ฅ๐ )) ๐๐๐๐๐ ๐0 , ๐1 , ๐2 , ๐3 , ๐ ๐๐๐๐๐ ๐0 , ๐1 , ๐2 , ๐3 ๐(๐(๐๐๐๐๐ [๐0 , ๐1 ] , ๐2 ), ๐3 ) ๐ ๐ ๐ ๐๐๐๐๐ ๐0 , ๐1 , ๐2 , ๐3 ๐ ๐ ๐ ๐ ๐๐๐๐๐ [ ] , ๐0 , ๐1 , ๐2 , ๐3 ๐ ๐ ๐ ๐ ๐, ๐0 , ๐1 , ๐2 , ๐3 ๐๐๐๐๐([๐0 , ๐1 , ๐2 , ๐3 ]) ๐(๐0 , ๐๐๐๐๐([๐1 , ๐2 , ๐3 ])) ๐(๐0 , ๐(๐1 , ๐๐๐๐๐([๐2 , ๐3 ]))) ๐(๐0 , ๐(๐1 , ๐(๐2 , ๐๐๐๐๐([๐3 ])))) ๐(๐0 , ๐(๐1 , ๐(๐2 , ๐(๐3 , ๐๐๐๐๐([]))))) ๐(๐0 , ๐(๐1 , ๐(๐2 , ๐(๐3 , ๐)))) Mathematical definition of right fold and left fold ๐๐๐๐๐ associates ๐ from the right ๐๐๐๐๐ associates ๐ from the left ๐ฅ = the Birst element ๐ฅs = all but the Birst element ๐ฅ = the last element ๐ฅ๐ = all but the last element