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

Left and Right Folds - Comparison of a mathematical definition and a programmatic one - Polyglot FP for Fun and Profit - Haskell and Scala

Left and Right Folds - Comparison of a mathematical definition and a programmatic one - Polyglot FP for Fun and Profit - Haskell andย Scala

We compare typical definitions of the left and right fold functions, with their mathematical definitions in Sergei Winitzkiโ€™s upcoming book: The Science of Functional Programming.

Errata:
Slide 13: "The way ๐‘“๐‘œ๐‘™๐‘‘๐‘™ does it is by associating to the right" - should, of course, end in "to the left".

Philip Schwarz

August 08, 2021
Tweet

More Decks by Philip Schwarz

Other Decks in Programming

Transcript

  1. Based on definitions from Left and Right Folds Comparison of

    a mathematical definition and a programmatic one Polyglot FP for Fun and Profit - Haskell and Scala @philip_schwarz slides by https://www.slideshare.net/pjschwarz Sergei Winitzki sergei-winitzki-11a6431 Richard Bird http://www.cs.ox.ac.uk/people/richard.bird/
  2. ๐‘“๐‘œ๐‘™๐‘‘๐‘™ โˆท ๐›ฝ โ†’ ๐›ผ โ†’ ๐›ฝ โ†’ ๐›ฝ โ†’

    ๐›ผ โ†’ ๐›ฝ ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘’ = ๐‘’ ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘’ ๐‘ฅ โˆถ ๐‘ฅ๐‘  = ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘“ ๐‘’ ๐‘ฅ ๐‘ฅ๐‘  ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ โˆท ๐›ผ โ†’ ๐›ฝ โ†’ ๐›ฝ โ†’ ๐›ฝ โ†’ ๐›ผ โ†’ ๐›ฝ ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘“ ๐‘’ = ๐‘’ ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘“ ๐‘’ ๐‘ฅ โˆถ ๐‘ฅ๐‘  = ๐‘“ ๐‘ฅ ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘“ ๐‘’ ๐‘ฅ๐‘  If I have to write down the definitions of a left fold and a right fold for lists, here is what I write While both definitions are recursive, the left fold is tail recursive, whereas the right fold isnโ€™t. Although I am very familiar with the above definitions, and view them as doing a good job of explaining the two folds, I am always interested in alternative ways of explaining things, and so I have been looking at Sergei Winitzkiโ€™s mathematical definitions of left and right folds, in his upcoming book: The Science of Functional Programming (SOFP). Sergeiโ€™s definitions of the folds are in the top two rows of the following table Sergei Winitzki Richard Bird @philip_schwarz
  3. test1 = TestCase (assertEqual "foldl(+) 0 []" 0 (foldl (+)

    0 [])) test2 = TestCase (assertEqual "foldl(+) 0 [1,2,3,4]" 10 (foldl (+) 0 [1,2,3,4])) test3 = TestCase (assertEqual "foldr (+) 0 []" 0 (foldr (+) 0 [])) test4 = TestCase (assertEqual "foldr (+) 0 [1,2,3,4]" 10 (foldr (+) 0 [1,2,3,4])) Left folds and right folds do not necessarily produce the same results. According to the first duality theorem of folding, one case in which the results are the same, is when we fold using the unit and associative operation of a monoid. First duality theorem. Suppose โŠ• is associative with unit ๐‘’. Then ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ โŠ• ๐‘’ ๐‘ฅ๐‘  = ๐‘“๐‘œ๐‘™๐‘‘๐‘™ โŠ• ๐‘’ ๐‘ฅ๐‘  For all finite lists ๐‘ฅ๐‘ . test5 = TestCase (assertEqual "foldl(-) 0 []" 0 (foldl (+) 0 [])) test6 = TestCase (assertEqual "foldl(-) 0 [1,2,3,4]" (- 10) (foldl (-) 0 [1,2,3,4])) test7 = TestCase (assertEqual "foldr (-) 0 []" 0 (foldr (-) 0 [])) test8 = TestCase (assertEqual "foldr (-) 0 [1,2,3,4]" (- 2) (foldr (-) 0 [1,2,3,4])) Folding integers left and right using the (Int,+,0) monoid, for example, produces the same results. But folding integers left and right using subtraction and zero, does not produce the same results, and in fact (Int,-,0) is not a monoid, because subtraction is not associative.
  4. ๐‘“ = ๐‘; ๐‘“ ๐‘ฅ โงบ ๐‘  = ๐‘”(๐‘ฅ, ๐‘“(๐‘ ))

    ๐‘“ = ๐‘; ๐‘“ ๐‘  โงบ [๐‘ฅ] = ๐‘”(๐‘“ ๐‘  , ๐‘ฅ) To avoid any confusion (the definitions use the same function name ๐‘“), and to align with the definitions on the right, letโ€™s modify Sergeiโ€™s definitions by doing some simple renaming. Here are Sergeiโ€™s mathematical definitions again, on the right (the โงบ operator is list concatenation). Notice how neither definition is tail recursive. That is deliberate. As Sergei explained to me: โ€œI'd like to avoid putting tail recursion into a mathematical formula, because tail recursion is just a detail of how we implement this functionโ€ and โ€œThe fact that foldLeft is tail recursive for List is an implementation detail that is specific to the List type. It will be different for other sequence types. I do not want to put the implementation details into the formulas.โ€ ๐‘“ โ†’ ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘” โ†’ ๐‘“ ๐‘ โ†’ ๐‘’ ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ = ๐‘’; ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘ฅ โงบ ๐‘  = ๐‘“(๐‘ฅ, ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ(๐‘ )) ๐‘“๐‘œ๐‘™๐‘‘๐‘™ = ๐‘’; ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘  โงบ [๐‘ฅ] = ๐‘“(๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘  , ๐‘ฅ) ๐‘“ โ†’ ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘” โ†’ ๐‘“ ๐‘ โ†’ ๐‘’ ๐‘“๐‘œ๐‘™๐‘‘๐‘™ โˆท ๐›ฝ โ†’ ๐›ผ โ†’ ๐›ฝ โ†’ ๐›ฝ โ†’ ๐›ผ โ†’ ๐›ฝ ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘’ = ๐‘’ ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘’ ๐‘ฅ โˆถ ๐‘ฅ๐‘  = ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘“ ๐‘’ ๐‘ฅ ๐‘ฅ๐‘  ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ โˆท ๐›ผ โ†’ ๐›ฝ โ†’ ๐›ฝ โ†’ ๐›ฝ โ†’ ๐›ผ โ†’ ๐›ฝ ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘“ ๐‘’ = ๐‘’ ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘“ ๐‘’ ๐‘ฅ โˆถ ๐‘ฅ๐‘  = ๐‘“ ๐‘ฅ ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘“ ๐‘’ ๐‘ฅ๐‘  ๐‘“ = ๐‘; ๐‘“ ๐‘ฅ โงบ ๐‘  = ๐‘”(๐‘ฅ, ๐‘“(๐‘ )) ๐‘“ = ๐‘; ๐‘“ ๐‘  โงบ [๐‘ฅ] = ๐‘”(๐‘“ ๐‘  , ๐‘ฅ)
  5. ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘“ ๐‘’ [ ] = ๐‘’ ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘“ ๐‘’

    ๐‘ฅ โงบ ๐‘  = ๐‘“ ๐‘ฅ (๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘“ ๐‘’ ๐‘ ) ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘’ = ๐‘’ ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘’ ๐‘  โงบ [๐‘ฅ] = ๐‘“ ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘’ ๐‘  ๐‘ฅ ๐‘“๐‘œ๐‘™๐‘‘๐‘™ :: (๐‘ โ†’ ๐‘Ž โ†’ ๐‘) โ†’ ๐‘ โ†’[๐‘Ž] โ†’ ๐‘ ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘’ = ๐‘’ ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘’ ๐‘Ž๐‘  = ๐‘“ ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘’ (๐‘–๐‘›๐‘–๐‘ก ๐‘Ž๐‘ ) (๐‘™๐‘Ž๐‘ ๐‘ก ๐‘Ž๐‘ ) ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ :: (๐‘Ž โ†’ ๐‘ โ†’ ๐‘) โ†’ ๐‘ โ†’[๐‘Ž] โ†’ ๐‘ ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘“ ๐‘’ = ๐‘’ ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘“ ๐‘’ ๐‘Ž๐‘  = ๐‘“ โ„Ž๐‘’๐‘Ž๐‘‘ ๐‘Ž๐‘  ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘“ ๐‘’ (๐‘ก๐‘Ž๐‘–๐‘™ ๐‘Ž๐‘ ) ๐‘“๐‘œ๐‘™๐‘‘๐‘™ :: (๐‘ โ†’ ๐‘Ž โ†’ ๐‘) โ†’ ๐‘ โ†’[๐‘Ž] โ†’ ๐‘ ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘’ = ๐‘’ ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘’ ๐‘Ž๐‘  = ๐‘“ ๐‘ ๐‘Ž ๐‘คโ„Ž๐‘’๐‘Ÿ๐‘’ ๐‘Ž = ๐‘™๐‘Ž๐‘ ๐‘ก ๐‘Ž๐‘  ๐‘ = ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘’ (๐‘–๐‘›๐‘–๐‘ก ๐‘Ž๐‘ ) ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ :: (๐‘Ž โ†’ ๐‘ โ†’ ๐‘) โ†’ ๐‘ โ†’[๐‘Ž] โ†’ ๐‘ ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘“ ๐‘’ = ๐‘’ ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘“ ๐‘’ ๐‘Ž๐‘  = ๐‘“ ๐‘Ž ๐‘ ๐‘คโ„Ž๐‘’๐‘Ÿ๐‘’ ๐‘Ž = โ„Ž๐‘’๐‘Ž๐‘‘ ๐‘Ž๐‘  ๐‘ = ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘“ ๐‘’ (๐‘ก๐‘Ž๐‘–๐‘™ ๐‘Ž๐‘ ) ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ = ๐‘’; ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘ฅ โงบ ๐‘  = ๐‘“(๐‘ฅ, ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ(๐‘ )) ๐‘“๐‘œ๐‘™๐‘‘๐‘™ = ๐‘’; ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘  โงบ [๐‘ฅ] = ๐‘“(๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘  , ๐‘ฅ) To help us understand the above two definitions, letโ€™s first express them in Haskell, pretending that we are able to use ๐‘  โงบ [๐‘ฅ] and ๐‘ฅ โงบ ๐‘  in a pattern match. Now letโ€™s replace ๐‘  โงบ [๐‘ฅ] and ๐‘ฅ โงบ ๐‘  with as, and get the functions to extract the ๐‘  and the ๐‘ฅ using the โ„Ž๐‘’๐‘Ž๐‘‘, ๐‘ก๐‘Ž๐‘–๐‘™, ๐‘™๐‘Ž๐‘ ๐‘ก and ๐‘–๐‘›๐‘–๐‘ก. Letโ€™s also add type signatures And now letโ€™s make it more obvious that what each fold is doing is taking an ๐‘Ž from the list, folding the rest of the list into a ๐‘, and then returning the result of calling ๐‘“ with ๐‘Ž and ๐‘.
  6. ๐‘“๐‘œ๐‘™๐‘‘๐‘™ :: (๐‘ โ†’ ๐‘Ž โ†’ ๐‘) โ†’ ๐‘ โ†’[๐‘Ž]

    โ†’ ๐‘ ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘’ = ๐‘’ ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘’ ๐‘Ž๐‘  = ๐‘“ ๐‘ ๐‘Ž ๐‘คโ„Ž๐‘’๐‘Ÿ๐‘’ ๐‘Ž = ๐‘™๐‘Ž๐‘ ๐‘ก ๐‘Ž๐‘  ๐‘ = ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘’ (๐‘–๐‘›๐‘–๐‘ก ๐‘Ž๐‘ ) ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ :: (๐‘Ž โ†’ ๐‘ โ†’ ๐‘) โ†’ ๐‘ โ†’[๐‘Ž] โ†’ ๐‘ ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘“ ๐‘’ = ๐‘’ ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘“ ๐‘’ ๐‘Ž๐‘  = ๐‘“ ๐‘Ž ๐‘ ๐‘คโ„Ž๐‘’๐‘Ÿ๐‘’ ๐‘Ž = โ„Ž๐‘’๐‘Ž๐‘‘ ๐‘Ž๐‘  ๐‘ = ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘“ ๐‘’ (๐‘ก๐‘Ž๐‘–๐‘™ ๐‘Ž๐‘ ) Since the above two functions are very similar, letโ€™s extract their common logic into a fold function. Letโ€™s call the function that is used to extract an element from the list ๐‘ก๐‘Ž๐‘˜๐‘’, and the function that is used to extract the rest of the list ๐‘Ÿ๐‘’๐‘ ๐‘ก. ๐‘“๐‘œ๐‘™๐‘‘ :: (๐‘ โ†’ ๐‘Ž โ†’ ๐‘) โ†’ ([๐‘Ž] โ†’ ๐‘Ž) โ†’ ([๐‘Ž] โ†’ [๐‘Ž]) โ†’ ๐‘ โ†’ [๐‘Ž] โ†’ ๐‘ ๐‘“๐‘œ๐‘™๐‘‘ ๐‘“ ๐‘ก๐‘Ž๐‘˜๐‘’ ๐‘Ÿ๐‘’๐‘ ๐‘ก ๐‘’ = ๐‘’ ๐‘“๐‘œ๐‘™๐‘‘ ๐‘“ ๐‘ก๐‘Ž๐‘˜๐‘’ ๐‘Ÿ๐‘’๐‘ ๐‘ก ๐‘’ ๐‘Ž๐‘  = ๐‘“ ๐‘ ๐‘Ž ๐‘คโ„Ž๐‘’๐‘Ÿ๐‘’ ๐‘Ž = ๐‘ก๐‘Ž๐‘˜๐‘’ ๐‘Ž๐‘  ๐‘ = ๐‘“๐‘œ๐‘™๐‘‘ ๐‘“ ๐‘ก๐‘Ž๐‘˜๐‘’ ๐‘Ÿ๐‘’๐‘ ๐‘ก ๐‘’ (๐‘Ÿ๐‘’๐‘ ๐‘ก ๐‘Ž๐‘ ) We can now define left and right folds in terms of ๐‘“๐‘œ๐‘™๐‘‘. ๐‘“๐‘œ๐‘™๐‘‘๐‘™ :: (๐‘ โ†’ ๐‘Ž โ†’ ๐‘) โ†’ ๐‘ โ†’[๐‘Ž] โ†’ ๐‘ ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ = ๐‘“๐‘œ๐‘™๐‘‘ ๐‘“ ๐‘™๐‘Ž๐‘ ๐‘ก ๐‘–๐‘›๐‘–๐‘ก ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ :: (๐‘Ž โ†’ ๐‘ โ†’ ๐‘) โ†’ ๐‘ โ†’[๐‘Ž] โ†’ ๐‘ ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘“ = ๐‘“๐‘œ๐‘™๐‘‘ ๐‘“๐‘™๐‘–๐‘ ๐‘“ โ„Ž๐‘’๐‘Ž๐‘‘ ๐‘ก๐‘Ž๐‘–๐‘™ ๐‘“๐‘™๐‘–๐‘ :: (๐‘Ž โ†’ ๐‘ โ†’ ๐‘) โ†’ ๐‘ โ†’ ๐‘Ž โ†’ ๐‘ ๐‘“๐‘™๐‘–๐‘ ๐‘“ ๐‘ฅ ๐‘ฆ = ๐‘“ ๐‘ฆ ๐‘ฅ The slightly perplexing thing is that while a left fold applies ๐‘“ to list elements starting with the โ„Ž๐‘’๐‘Ž๐‘‘ of the list and proceeding from left to right, the above ๐‘“๐‘œ๐‘™๐‘‘๐‘™ function achieves that by navigating through list elements from right to left. Third duality theorem. For all finite lists ๐‘ฅ๐‘ , ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘“ ๐‘’ ๐‘ฅ๐‘  = ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“๐‘™๐‘–๐‘ ๐‘“ ๐‘’ (๐‘Ÿ๐‘’๐‘ฃ๐‘’๐‘Ÿ๐‘ ๐‘’ ๐‘ฅ๐‘ ) ๐’˜๐’‰๐’†๐’“๐’† ๐‘“๐‘™๐‘–๐‘ ๐‘“ ๐‘ฅ ๐‘ฆ = ๐‘“ ๐‘ฆ ๐‘ฅ The fact that we can define ๐‘“๐‘œ๐‘™๐‘‘๐‘™ and ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ in terms of ๐‘“๐‘œ๐‘™๐‘‘, as we do above, seems related to the third duality theorem of folding. Instead of our ๐‘“๐‘œ๐‘™๐‘‘๐‘™ function being passed the reverse of the list passed to ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ, it processes the list with ๐‘™๐‘Ž๐‘ ๐‘ก and ๐‘–๐‘›๐‘–๐‘ก, rather than with โ„Ž๐‘’๐‘Ž๐‘‘ and ๐‘ก๐‘Ž๐‘–๐‘™. @philip_schwarz
  7. To summarise what we did to help understand SOFPโ€™s mathematical

    definitions of left and right fold: we turned them into code and expressed them in terms of a common function ๐‘“๐‘œ๐‘™๐‘‘ that uses a ๐‘ก๐‘Ž๐‘˜๐‘’ function to extract an element from the list being folded, and a ๐‘Ÿ๐‘’๐‘ ๐‘ก function to extract the rest of the list. ๐‘“๐‘œ๐‘™๐‘‘ :: (๐‘ โ†’ ๐‘Ž โ†’ ๐‘) โ†’ ([๐‘Ž] โ†’ ๐‘Ž) โ†’ ([๐‘Ž] โ†’ [๐‘Ž]) โ†’ ๐‘ โ†’ [๐‘Ž] โ†’ ๐‘ ๐‘“๐‘œ๐‘™๐‘‘ ๐‘“ ๐‘ก๐‘Ž๐‘˜๐‘’ ๐‘Ÿ๐‘’๐‘ ๐‘ก ๐‘’ = ๐‘’ ๐‘“๐‘œ๐‘™๐‘‘ ๐‘“ ๐‘ก๐‘Ž๐‘˜๐‘’ ๐‘Ÿ๐‘’๐‘ ๐‘ก ๐‘’ ๐‘Ž๐‘  = ๐‘“ ๐‘ ๐‘Ž ๐‘คโ„Ž๐‘’๐‘Ÿ๐‘’ ๐‘Ž = ๐‘ก๐‘Ž๐‘˜๐‘’ ๐‘Ž๐‘  ๐‘ = ๐‘“๐‘œ๐‘™๐‘‘ ๐‘“ ๐‘ก๐‘Ž๐‘˜๐‘’ ๐‘Ÿ๐‘’๐‘ ๐‘ก ๐‘’ (๐‘Ÿ๐‘’๐‘ ๐‘ก ๐‘Ž๐‘ ) ๐‘“๐‘œ๐‘™๐‘‘๐‘™ :: (๐‘ โ†’ ๐‘Ž โ†’ ๐‘) โ†’ ๐‘ โ†’[๐‘Ž] โ†’ ๐‘ ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ = ๐‘“๐‘œ๐‘™๐‘‘ ๐‘“ ๐‘™๐‘Ž๐‘ ๐‘ก ๐‘–๐‘›๐‘–๐‘ก ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ :: (๐‘Ž โ†’ ๐‘ โ†’ ๐‘) โ†’ ๐‘ โ†’[๐‘Ž] โ†’ ๐‘ ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘“ = ๐‘“๐‘œ๐‘™๐‘‘ ๐‘“๐‘™๐‘–๐‘ ๐‘“ โ„Ž๐‘’๐‘Ž๐‘‘ ๐‘ก๐‘Ž๐‘–๐‘™ ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ = ๐‘’; ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘ฅ โงบ ๐‘  = ๐‘“(๐‘ฅ, ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ(๐‘ )) ๐‘“๐‘œ๐‘™๐‘‘๐‘™ = ๐‘’; ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘  โงบ [๐‘ฅ] = ๐‘“(๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘  , ๐‘ฅ) ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ = ๐‘’; ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘Ž๐‘  = ๐‘“(โ„Ž๐‘’๐‘Ž๐‘‘(๐‘Ž๐‘ ), ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ(๐‘ก๐‘Ž๐‘–๐‘™(๐‘Ž๐‘ ))) ๐‘“๐‘œ๐‘™๐‘‘๐‘™ = ๐‘’; ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘Ž๐‘  = ๐‘“(๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘–๐‘›๐‘–๐‘ก(๐‘Ž๐‘ ) , ๐‘™๐‘Ž๐‘ ๐‘ก(๐‘Ž๐‘ )) Letโ€™s feed one aspect of the above back into Sergeiโ€™s definitions. Letโ€™s temporarily rewrite them by replacing ๐‘  โงบ [๐‘ฅ] and ๐‘ฅ โงบ ๐‘  with as, and getting the definitions to extract the ๐‘  and the ๐‘ฅ using the functions โ„Ž๐‘’๐‘Ž๐‘‘, ๐‘ก๐‘Ž๐‘–๐‘™, ๐‘™๐‘Ž๐‘ ๐‘ก and ๐‘–๐‘›๐‘–๐‘ก. Notice how the flipping of ๐‘“ done by the ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ function above, is reflected, in the ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ function below, in the fact that its ๐‘“ takes an ๐‘Ž and a ๐‘, rather than a ๐‘ and an ๐‘Ž.
  8. Another thing we can do to understand SOFPโ€™s definitions of

    left and right folds, is to see how they work when applied to a sample list, e.g. [๐‘ฅ0 , ๐‘ฅ1 , ๐‘ฅ2 , ๐‘ฅ3 ], when we run them manually. In the next slide we first do this for the following definitions ๐‘“๐‘œ๐‘™๐‘‘๐‘™ = ๐‘’; ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘  โงบ [๐‘ฅ] = ๐‘“(๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘  , ๐‘ฅ) ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ = ๐‘’; ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘ฅ โงบ ๐‘  = ๐‘“(๐‘ฅ, ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ(๐‘ )) ๐‘“๐‘œ๐‘™๐‘‘๐‘™ โˆท ๐›ฝ โ†’ ๐›ผ โ†’ ๐›ฝ โ†’ ๐›ฝ โ†’ ๐›ผ โ†’ ๐›ฝ ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘’ = ๐‘’ ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘’ ๐‘ฅ โˆถ ๐‘ฅ๐‘  = ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘“ ๐‘’ ๐‘ฅ ๐‘ฅ๐‘  ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ โˆท ๐›ผ โ†’ ๐›ฝ โ†’ ๐›ฝ โ†’ ๐›ฝ โ†’ ๐›ผ โ†’ ๐›ฝ ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘“ ๐‘’ = ๐‘’ ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘“ ๐‘’ ๐‘ฅ โˆถ ๐‘ฅ๐‘  = ๐‘“ ๐‘ฅ ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘“ ๐‘’ ๐‘ฅ๐‘  In the slide after that, we do it for SOFPโ€™s definitions.
  9. โˆถ / \ ๐‘ฅ0 โˆถ / \ ๐‘ฅ1 โˆถ /

    \ ๐‘ฅ2 โˆถ / \ ๐‘ฅ3 ๐‘“ / \ ๐‘“ ๐‘ฅ3 / \ ๐‘“ ๐‘ฅ2 / \ ๐‘“ ๐‘ฅ1 / \ ๐‘’ ๐‘ฅ0 ๐‘“ / \ ๐‘ฅ0 ๐‘“ / \ ๐‘ฅ1 ๐‘“ / \ ๐‘ฅ2 ๐‘“ / \ ๐‘ฅ3 ๐‘’ ๐‘ฅ0 : (๐‘ฅ1 : ๐‘ฅ2 : ๐‘ฅ3 : ) ๐‘“ ๐‘“ ๐‘“ ๐‘“ ๐‘’ ๐‘ฅ0 ๐‘ฅ1 ๐‘ฅ2 ๐‘ฅ3 var ๐‘Ž๐‘๐‘ = ๐‘’ foreach(๐‘ฅ in ๐‘ฅs) ๐‘Ž๐‘๐‘ = ๐‘“(๐‘Ž๐‘๐‘, ๐‘ฅ) 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
  10. โˆถ / \ ๐‘ฅ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 , ๐‘’))))
  11. ๐‘“๐‘œ๐‘™๐‘‘๐‘™ โˆท ๐›ฝ โ†’ ๐›ผ โ†’ ๐›ฝ โ†’ ๐›ฝ โ†’

    ๐›ผ โ†’ ๐›ฝ ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘’ = ๐‘’ ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘’ ๐‘ฅ: ๐‘ฅ๐‘  = ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘“ ๐‘’ ๐‘ฅ ๐‘ฅ๐‘  ๐‘“๐‘œ๐‘™๐‘‘๐‘™ = ๐‘’; ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘  โงบ [๐‘ฅ] = ๐‘“(๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘  , ๐‘ฅ) โˆถ / \ ๐‘ฅ0 โˆถ / \ ๐‘ฅ1 โˆถ / \ ๐‘ฅ2 โˆถ / \ ๐‘ฅ3 ๐‘ฅ0 : (๐‘ฅ1 : ๐‘ฅ2 : ๐‘ฅ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 Now letโ€™s compare the results for both definitions of ๐‘“๐‘œ๐‘™๐‘‘๐‘™. The results are the same. @philip_schwarz
  12. ๐‘“ / \ ๐‘ฅ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 ] And now letโ€™s compare the results for both definitions of ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ. Again, the results are the same.
  13. The way ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ applies ๐‘“ to ๐‘ฅ, ๐‘ฆ, z is

    by associating to the right. The way ๐‘“๐‘œ๐‘™๐‘‘๐‘™ does it is by associating to the right. Thinking of ๐‘“ as an infix operator can help to grasp this. ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘“(๐‘ฅ, ๐‘“(๐‘ฆ, ๐‘ง)) ๐‘ฅ โŠ• ๐‘ฆ โŠ• ๐‘ง ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘“ ๐‘ฅ, ๐‘ฆ , ๐‘ง (๐‘ฅ โŠ• ๐‘ฆ) โŠ• ๐‘ง ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ โŠ• ๐‘’ ๐‘ฅ, ๐‘ฆ, z = ๐‘ฅ โŠ• (๐‘ฆ โŠ• ๐‘ง โŠ• ๐‘’ ) ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ (+) 0 [1,2,3] = 1 + (2 + (3 + 0)) = 6 ๐‘“๐‘œ๐‘™๐‘‘๐‘™ (+) 0 [1,2,3] = ((0 + 1) + 2) + 3 = 6 ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ โˆ’ 0 1,2,3 = 1 โˆ’ (2 โˆ’ (3 โˆ’ 0)) = 2 ๐‘“๐‘œ๐‘™๐‘‘๐‘™ โˆ’ 0 1,2,3 = ((0 โˆ’ 1) โˆ’ 2) โˆ’ 3 = โˆ’6 โŠ• / \ ๐‘ฅ โŠ• / \ ๐‘ฆ โŠ• / \ ๐‘ง ๐‘’ โŠ• / \ โŠ• ๐‘ง / \ โŠ• ๐‘ฆ / \ ๐‘’ ๐‘ฅ ๐‘“๐‘œ๐‘™๐‘‘๐‘™ โŠ• ๐‘’ ๐‘ฅ, ๐‘ฆ, ๐‘ง = ((๐‘’ โŠ• ๐‘ฅ) โŠ• ๐‘ฆ) โŠ• ๐‘ง Addition is associative, so associating to the left and to the right yields the same result. But subtraction isnโ€™t associative, so associating to the left yields a result that is different to the one yielded when associating to the right.
  14. We have seen that Sergeiโ€™s definitions of left and right

    folds make perfect sense. Not only are they simple and succinct, they are also free of implementation details like tail recursion. Thatโ€™s all. I hope you found this slide deck useful. By the way, in case you are interested, see below for a whole series of slide decks dedicated to folding.