Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Folding Cheat Sheet #2

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

Avatar for Philip Schwarz

Philip Schwarz PRO

March 30, 2024
Tweet

More Decks by Philip Schwarz

Other Decks in Programming

Transcript

  1. CHEAT-SHEET Folding #2 โˆถ / \ ๐’‚๐ŸŽ โˆถ / \

    ๐’‚๐Ÿ โˆถ / \ ๐’‚๐Ÿ โˆถ / \ ๐’‚๐Ÿ‘ ๐’‡ / \ ๐’‚๐ŸŽ ๐’‡ / \ ๐’‚๐Ÿ ๐’‡ / \ ๐’‚๐Ÿ ๐’‡ / \ ๐’‚๐Ÿ‘ ๐’† @philip_schwarz slides by https://fpilluminated.com/
  2. โˆถ / \ ๐‘Ž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
  3. โˆถ / \ ๐‘Ž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