SlideShare a Scribd company logo
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/
๐‘“๐‘œ๐‘™๐‘‘๐‘™ โˆท ๐›ฝ โ†’ ๐›ผ โ†’ ๐›ฝ โ†’ ๐›ฝ โ†’ ๐›ผ โ†’ ๐›ฝ
๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘’ = ๐‘’
๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘’ ๐‘ฅ โˆถ ๐‘ฅ๐‘  = ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘“ ๐‘’ ๐‘ฅ ๐‘ฅ๐‘ 
๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ โˆท ๐›ผ โ†’ ๐›ฝ โ†’ ๐›ฝ โ†’ ๐›ฝ โ†’ ๐›ผ โ†’ ๐›ฝ
๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘“ ๐‘’ = ๐‘’
๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘“ ๐‘’ ๐‘ฅ โˆถ ๐‘ฅ๐‘  = ๐‘“ ๐‘ฅ ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘“ ๐‘’ ๐‘ฅ๐‘ 
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
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.
๐‘“ = ๐‘; ๐‘“ ๐‘ฅ โงบ ๐‘  = ๐‘”(๐‘ฅ, ๐‘“(๐‘ ))
๐‘“ = ๐‘; ๐‘“ ๐‘  โงบ [๐‘ฅ] = ๐‘”(๐‘“ ๐‘  , ๐‘ฅ)
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.โ€
๐‘“ โ†’ ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘” โ†’ ๐‘“ ๐‘ โ†’ ๐‘’
๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ = ๐‘’; ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘ฅ โงบ ๐‘  = ๐‘“(๐‘ฅ, ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ(๐‘ ))
๐‘“๐‘œ๐‘™๐‘‘๐‘™ = ๐‘’; ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘  โงบ [๐‘ฅ] = ๐‘“(๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘  , ๐‘ฅ)
๐‘“ โ†’ ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘” โ†’ ๐‘“ ๐‘ โ†’ ๐‘’
๐‘“๐‘œ๐‘™๐‘‘๐‘™ โˆท ๐›ฝ โ†’ ๐›ผ โ†’ ๐›ฝ โ†’ ๐›ฝ โ†’ ๐›ผ โ†’ ๐›ฝ
๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘’ = ๐‘’
๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘’ ๐‘ฅ โˆถ ๐‘ฅ๐‘  = ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘“ ๐‘’ ๐‘ฅ ๐‘ฅ๐‘ 
๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ โˆท ๐›ผ โ†’ ๐›ฝ โ†’ ๐›ฝ โ†’ ๐›ฝ โ†’ ๐›ผ โ†’ ๐›ฝ
๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘“ ๐‘’ = ๐‘’
๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘“ ๐‘’ ๐‘ฅ โˆถ ๐‘ฅ๐‘  = ๐‘“ ๐‘ฅ ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘“ ๐‘’ ๐‘ฅ๐‘ 
๐‘“ = ๐‘; ๐‘“ ๐‘ฅ โงบ ๐‘  = ๐‘”(๐‘ฅ, ๐‘“(๐‘ ))
๐‘“ = ๐‘; ๐‘“ ๐‘  โงบ [๐‘ฅ] = ๐‘”(๐‘“ ๐‘  , ๐‘ฅ)
๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘“ ๐‘’ [ ] = ๐‘’
๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘“ ๐‘’ ๐‘ฅ โงบ ๐‘  = ๐‘“ ๐‘ฅ (๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘“ ๐‘’ ๐‘ )
๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘’ = ๐‘’
๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘’ ๐‘  โงบ [๐‘ฅ] = ๐‘“ ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘’ ๐‘  ๐‘ฅ
๐‘“๐‘œ๐‘™๐‘‘๐‘™ :: (๐‘ โ†’ ๐‘Ž โ†’ ๐‘) โ†’ ๐‘ โ†’[๐‘Ž] โ†’ ๐‘
๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘’ = ๐‘’
๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘’ ๐‘Ž๐‘  = ๐‘“ ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘’ (๐‘–๐‘›๐‘–๐‘ก ๐‘Ž๐‘ ) (๐‘™๐‘Ž๐‘ ๐‘ก ๐‘Ž๐‘ )
๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ :: (๐‘Ž โ†’ ๐‘ โ†’ ๐‘) โ†’ ๐‘ โ†’[๐‘Ž] โ†’ ๐‘
๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘“ ๐‘’ = ๐‘’
๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘“ ๐‘’ ๐‘Ž๐‘  = ๐‘“ โ„Ž๐‘’๐‘Ž๐‘‘ ๐‘Ž๐‘  ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘“ ๐‘’ (๐‘ก๐‘Ž๐‘–๐‘™ ๐‘Ž๐‘ )
๐‘“๐‘œ๐‘™๐‘‘๐‘™ :: (๐‘ โ†’ ๐‘Ž โ†’ ๐‘) โ†’ ๐‘ โ†’[๐‘Ž] โ†’ ๐‘
๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘’ = ๐‘’
๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘’ ๐‘Ž๐‘  = ๐‘“ ๐‘ ๐‘Ž
๐‘คโ„Ž๐‘’๐‘Ÿ๐‘’ ๐‘Ž = ๐‘™๐‘Ž๐‘ ๐‘ก ๐‘Ž๐‘ 
๐‘ = ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘’ (๐‘–๐‘›๐‘–๐‘ก ๐‘Ž๐‘ )
๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ :: (๐‘Ž โ†’ ๐‘ โ†’ ๐‘) โ†’ ๐‘ โ†’[๐‘Ž] โ†’ ๐‘
๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘“ ๐‘’ = ๐‘’
๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘“ ๐‘’ ๐‘Ž๐‘  = ๐‘“ ๐‘Ž ๐‘
๐‘คโ„Ž๐‘’๐‘Ÿ๐‘’ ๐‘Ž = โ„Ž๐‘’๐‘Ž๐‘‘ ๐‘Ž๐‘ 
๐‘ = ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘“ ๐‘’ (๐‘ก๐‘Ž๐‘–๐‘™ ๐‘Ž๐‘ )
๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ = ๐‘’; ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘ฅ โงบ ๐‘  = ๐‘“(๐‘ฅ, ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ(๐‘ ))
๐‘“๐‘œ๐‘™๐‘‘๐‘™ = ๐‘’; ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘  โงบ [๐‘ฅ] = ๐‘“(๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘  , ๐‘ฅ)
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 ๐‘.
๐‘“๐‘œ๐‘™๐‘‘๐‘™ :: (๐‘ โ†’ ๐‘Ž โ†’ ๐‘) โ†’ ๐‘ โ†’[๐‘Ž] โ†’ ๐‘
๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘’ = ๐‘’
๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘’ ๐‘Ž๐‘  = ๐‘“ ๐‘ ๐‘Ž
๐‘คโ„Ž๐‘’๐‘Ÿ๐‘’ ๐‘Ž = ๐‘™๐‘Ž๐‘ ๐‘ก ๐‘Ž๐‘ 
๐‘ = ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘’ (๐‘–๐‘›๐‘–๐‘ก ๐‘Ž๐‘ )
๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ :: (๐‘Ž โ†’ ๐‘ โ†’ ๐‘) โ†’ ๐‘ โ†’[๐‘Ž] โ†’ ๐‘
๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘“ ๐‘’ = ๐‘’
๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘“ ๐‘’ ๐‘Ž๐‘  = ๐‘“ ๐‘Ž ๐‘
๐‘คโ„Ž๐‘’๐‘Ÿ๐‘’ ๐‘Ž = โ„Ž๐‘’๐‘Ž๐‘‘ ๐‘Ž๐‘ 
๐‘ = ๐‘“๐‘œ๐‘™๐‘‘๐‘Ÿ ๐‘“ ๐‘’ (๐‘ก๐‘Ž๐‘–๐‘™ ๐‘Ž๐‘ )
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
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 ๐‘Ž.
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.
โˆถ
/ 
๐‘ฅ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
โˆถ
/ 
๐‘ฅ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, ๐‘’))))
๐‘“๐‘œ๐‘™๐‘‘๐‘™ โˆท ๐›ฝ โ†’ ๐›ผ โ†’ ๐›ฝ โ†’ ๐›ฝ โ†’ ๐›ผ โ†’ ๐›ฝ
๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘’ = ๐‘’
๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘’ ๐‘ฅ: ๐‘ฅ๐‘  = ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘“ ๐‘“ ๐‘’ ๐‘ฅ ๐‘ฅ๐‘ 
๐‘“๐‘œ๐‘™๐‘‘๐‘™ = ๐‘’; ๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘  โงบ [๐‘ฅ] = ๐‘“(๐‘“๐‘œ๐‘™๐‘‘๐‘™ ๐‘  , ๐‘ฅ)
โˆถ
/ 
๐‘ฅ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
๐‘“
/ 
๐‘ฅ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.
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.
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.

More Related Content

What's hot

Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...
Philip Schwarz
ย 
Functional Programming 101 with Scala and ZIO @FunctionalWorld
Functional Programming 101 with Scala and ZIO @FunctionalWorldFunctional Programming 101 with Scala and ZIO @FunctionalWorld
Functional Programming 101 with Scala and ZIO @FunctionalWorld
Jorge Vรกsquez
ย 
Applicative style programming
Applicative style programmingApplicative style programming
Applicative style programming
Josรฉ Luis Garcรญa Hernรกndez
ย 
Functional Core and Imperative Shell - Game of Life Example - Haskell and Scala
Functional Core and Imperative Shell - Game of Life Example - Haskell and ScalaFunctional Core and Imperative Shell - Game of Life Example - Haskell and Scala
Functional Core and Imperative Shell - Game of Life Example - Haskell and Scala
Philip Schwarz
ย 
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2
Philip Schwarz
ย 
Monoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and CatsMonoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and Cats
Philip Schwarz
ย 
Blazing Fast, Pure Effects without Monads โ€” LambdaConf 2018
Blazing Fast, Pure Effects without Monads โ€” LambdaConf 2018Blazing Fast, Pure Effects without Monads โ€” LambdaConf 2018
Blazing Fast, Pure Effects without Monads โ€” LambdaConf 2018
John De Goes
ย 
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
Philip Schwarz
ย 
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...
Philip Schwarz
ย 
Implementing the IO Monad in Scala
Implementing the IO Monad in ScalaImplementing the IO Monad in Scala
Implementing the IO Monad in Scala
Hermann Hueck
ย 
The Uniform Access Principle
The Uniform Access PrincipleThe Uniform Access Principle
The Uniform Access Principle
Philip Schwarz
ย 
The Expression Problem - Part 2
The Expression Problem - Part 2The Expression Problem - Part 2
The Expression Problem - Part 2
Philip Schwarz
ย 
Arriving at monads by going from pure-function composition to effectful-funct...
Arriving at monads by going from pure-function composition to effectful-funct...Arriving at monads by going from pure-function composition to effectful-funct...
Arriving at monads by going from pure-function composition to effectful-funct...
Philip Schwarz
ย 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...
Philip Schwarz
ย 
Sierpinski Triangle - Polyglot FP for Fun and Profit - Haskell and Scala
Sierpinski Triangle - Polyglot FP for Fun and Profit - Haskell and ScalaSierpinski Triangle - Polyglot FP for Fun and Profit - Haskell and Scala
Sierpinski Triangle - Polyglot FP for Fun and Profit - Haskell and Scala
Philip Schwarz
ย 
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...
Philip Schwarz
ย 
Purely Functional Data Structures in Scala
Purely Functional Data Structures in ScalaPurely Functional Data Structures in Scala
Purely Functional Data Structures in Scala
Vladimir Kostyukov
ย 
ZIO-Direct - Functional Scala 2022
ZIO-Direct - Functional Scala 2022ZIO-Direct - Functional Scala 2022
ZIO-Direct - Functional Scala 2022
Alexander Ioffe
ย 
Exploring ZIO Prelude: The game changer for typeclasses in Scala
Exploring ZIO Prelude: The game changer for typeclasses in ScalaExploring ZIO Prelude: The game changer for typeclasses in Scala
Exploring ZIO Prelude: The game changer for typeclasses in Scala
Jorge Vรกsquez
ย 
Computer Graphics in Java and Scala - Part 1
Computer Graphics in Java and Scala - Part 1Computer Graphics in Java and Scala - Part 1
Computer Graphics in Java and Scala - Part 1
Philip Schwarz
ย 

What's hot (20)

Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - with ...
ย 
Functional Programming 101 with Scala and ZIO @FunctionalWorld
Functional Programming 101 with Scala and ZIO @FunctionalWorldFunctional Programming 101 with Scala and ZIO @FunctionalWorld
Functional Programming 101 with Scala and ZIO @FunctionalWorld
ย 
Applicative style programming
Applicative style programmingApplicative style programming
Applicative style programming
ย 
Functional Core and Imperative Shell - Game of Life Example - Haskell and Scala
Functional Core and Imperative Shell - Game of Life Example - Haskell and ScalaFunctional Core and Imperative Shell - Game of Life Example - Haskell and Scala
Functional Core and Imperative Shell - Game of Life Example - Haskell and Scala
ย 
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 2
ย 
Monoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and CatsMonoids - Part 1 - with examples using Scalaz and Cats
Monoids - Part 1 - with examples using Scalaz and Cats
ย 
Blazing Fast, Pure Effects without Monads โ€” LambdaConf 2018
Blazing Fast, Pure Effects without Monads โ€” LambdaConf 2018Blazing Fast, Pure Effects without Monads โ€” LambdaConf 2018
Blazing Fast, Pure Effects without Monads โ€” LambdaConf 2018
ย 
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 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...
ย 
Implementing the IO Monad in Scala
Implementing the IO Monad in ScalaImplementing the IO Monad in Scala
Implementing the IO Monad in Scala
ย 
The Uniform Access Principle
The Uniform Access PrincipleThe Uniform Access Principle
The Uniform Access Principle
ย 
The Expression Problem - Part 2
The Expression Problem - Part 2The Expression Problem - Part 2
The Expression Problem - Part 2
ย 
Arriving at monads by going from pure-function composition to effectful-funct...
Arriving at monads by going from pure-function composition to effectful-funct...Arriving at monads by going from pure-function composition to effectful-funct...
Arriving at monads by going from pure-function composition to effectful-funct...
ย 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala Part 2 ...
ย 
Sierpinski Triangle - Polyglot FP for Fun and Profit - Haskell and Scala
Sierpinski Triangle - Polyglot FP for Fun and Profit - Haskell and ScalaSierpinski Triangle - Polyglot FP for Fun and Profit - Haskell and Scala
Sierpinski Triangle - Polyglot FP for Fun and Profit - Haskell and Scala
ย 
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...
ย 
Purely Functional Data Structures in Scala
Purely Functional Data Structures in ScalaPurely Functional Data Structures in Scala
Purely Functional Data Structures in Scala
ย 
ZIO-Direct - Functional Scala 2022
ZIO-Direct - Functional Scala 2022ZIO-Direct - Functional Scala 2022
ZIO-Direct - Functional Scala 2022
ย 
Exploring ZIO Prelude: The game changer for typeclasses in Scala
Exploring ZIO Prelude: The game changer for typeclasses in ScalaExploring ZIO Prelude: The game changer for typeclasses in Scala
Exploring ZIO Prelude: The game changer for typeclasses in Scala
ย 
Computer Graphics in Java and Scala - Part 1
Computer Graphics in Java and Scala - Part 1Computer Graphics in Java and Scala - Part 1
Computer Graphics in Java and Scala - Part 1
ย 

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

Folding Cheat Sheet #3 - third in a series
Folding Cheat Sheet #3 - third in a seriesFolding Cheat Sheet #3 - third in a series
Folding Cheat Sheet #3 - third in a series
Philip Schwarz
ย 
Dual Spaces of Generalized Cesaro Sequence Space and Related Matrix Mapping
Dual Spaces of Generalized Cesaro Sequence Space and Related Matrix MappingDual Spaces of Generalized Cesaro Sequence Space and Related Matrix Mapping
Dual Spaces of Generalized Cesaro Sequence Space and Related Matrix Mapping
inventionjournals
ย 
Differential Geometry for Machine Learning
Differential Geometry for Machine LearningDifferential Geometry for Machine Learning
Differential Geometry for Machine Learning
SEMINARGROOT
ย 
Basic calculus (ii) recap
Basic calculus (ii) recapBasic calculus (ii) recap
Basic calculus (ii) recap
Farzad Javidanrad
ย 
Semana 24 funciones iv รกlgebra uni ccesa007
Semana 24 funciones iv รกlgebra uni ccesa007Semana 24 funciones iv รกlgebra uni ccesa007
Semana 24 funciones iv รกlgebra uni ccesa007
Demetrio Ccesa Rayme
ย 
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 ๐€ โˆ’ ๐’‹๐’†๐’„๐’•๐’Š๐’๐’. โ€“ ...
IJMER
ย 
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
Philip Schwarz
ย 
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
Philip Schwarz
ย 
E0561719
E0561719E0561719
E0561719
IOSR Journals
ย 
Teoria Numรฉrica (Palestra 01)
Teoria Numรฉrica (Palestra 01)Teoria Numรฉrica (Palestra 01)
Teoria Numรฉrica (Palestra 01)
Eugenio Souza
ย 
PRODUCT RULES
PRODUCT RULESPRODUCT RULES
PRODUCT RULES
NumanUsama
ย 
Matrix Transformations on Some Difference Sequence Spaces
Matrix Transformations on Some Difference Sequence SpacesMatrix Transformations on Some Difference Sequence Spaces
Matrix Transformations on Some Difference Sequence Spaces
IOSR Journals
ย 
Recurrence relation of Bessel's and Legendre's function
Recurrence relation of Bessel's and Legendre's functionRecurrence relation of Bessel's and Legendre's function
Recurrence relation of Bessel's and Legendre's function
Partho Ghosh
ย 
3 capitulo-iii-matriz-asociada-sem-14-t-l-d
3 capitulo-iii-matriz-asociada-sem-14-t-l-d3 capitulo-iii-matriz-asociada-sem-14-t-l-d
3 capitulo-iii-matriz-asociada-sem-14-t-l-d
FernandoDanielMamani1
ย 
Further Results On The Basis Of Cauchyโ€™s Proper Bound for the Zeros of Entire...
Further Results On The Basis Of Cauchyโ€™s Proper Bound for the Zeros of Entire...Further Results On The Basis Of Cauchyโ€™s Proper Bound for the Zeros of Entire...
Further Results On The Basis Of Cauchyโ€™s Proper Bound for the Zeros of Entire...
IJMER
ย 
Generalized Laplace - Mellin Integral Transformation
Generalized Laplace - Mellin Integral TransformationGeneralized Laplace - Mellin Integral Transformation
Generalized Laplace - Mellin Integral Transformation
IJERA Editor
ย 
Relativity
RelativityRelativity
Relativity
edgardoangeles1
ย 
Change variablethm
Change variablethmChange variablethm
Change variablethm
Jasonleav
ย 
AJMS_480_23.pdf
AJMS_480_23.pdfAJMS_480_23.pdf
AJMS_480_23.pdf
BRNSS Publication Hub
ย 
MT102 ะ›ะตะบั† 4
MT102 ะ›ะตะบั† 4MT102 ะ›ะตะบั† 4
MT102 ะ›ะตะบั† 4
ssuser184df1
ย 

Similar to Left and Right Folds - Comparison of a mathematical definition and a programmatic one - Polyglot FP for Fun and Profit - Haskell and Scala (20)

Folding Cheat Sheet #3 - third in a series
Folding Cheat Sheet #3 - third in a seriesFolding Cheat Sheet #3 - third in a series
Folding Cheat Sheet #3 - third in a series
ย 
Dual Spaces of Generalized Cesaro Sequence Space and Related Matrix Mapping
Dual Spaces of Generalized Cesaro Sequence Space and Related Matrix MappingDual Spaces of Generalized Cesaro Sequence Space and Related Matrix Mapping
Dual Spaces of Generalized Cesaro Sequence Space and Related Matrix Mapping
ย 
Differential Geometry for Machine Learning
Differential Geometry for Machine LearningDifferential Geometry for Machine Learning
Differential Geometry for Machine Learning
ย 
Basic calculus (ii) recap
Basic calculus (ii) recapBasic calculus (ii) recap
Basic calculus (ii) recap
ย 
Semana 24 funciones iv รกlgebra uni ccesa007
Semana 24 funciones iv รกlgebra uni ccesa007Semana 24 funciones iv รกlgebra uni ccesa007
Semana 24 funciones iv รกlgebra uni ccesa007
ย 
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 ๐€ โˆ’ ๐’‹๐’†๐’„๐’•๐’Š๐’๐’. โ€“ ...
ย 
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
ย 
E0561719
E0561719E0561719
E0561719
ย 
Teoria Numรฉrica (Palestra 01)
Teoria Numรฉrica (Palestra 01)Teoria Numรฉrica (Palestra 01)
Teoria Numรฉrica (Palestra 01)
ย 
PRODUCT RULES
PRODUCT RULESPRODUCT RULES
PRODUCT RULES
ย 
Matrix Transformations on Some Difference Sequence Spaces
Matrix Transformations on Some Difference Sequence SpacesMatrix Transformations on Some Difference Sequence Spaces
Matrix Transformations on Some Difference Sequence Spaces
ย 
Recurrence relation of Bessel's and Legendre's function
Recurrence relation of Bessel's and Legendre's functionRecurrence relation of Bessel's and Legendre's function
Recurrence relation of Bessel's and Legendre's function
ย 
3 capitulo-iii-matriz-asociada-sem-14-t-l-d
3 capitulo-iii-matriz-asociada-sem-14-t-l-d3 capitulo-iii-matriz-asociada-sem-14-t-l-d
3 capitulo-iii-matriz-asociada-sem-14-t-l-d
ย 
Further Results On The Basis Of Cauchyโ€™s Proper Bound for the Zeros of Entire...
Further Results On The Basis Of Cauchyโ€™s Proper Bound for the Zeros of Entire...Further Results On The Basis Of Cauchyโ€™s Proper Bound for the Zeros of Entire...
Further Results On The Basis Of Cauchyโ€™s Proper Bound for the Zeros of Entire...
ย 
Generalized Laplace - Mellin Integral Transformation
Generalized Laplace - Mellin Integral TransformationGeneralized Laplace - Mellin Integral Transformation
Generalized Laplace - Mellin Integral Transformation
ย 
Relativity
RelativityRelativity
Relativity
ย 
Change variablethm
Change variablethmChange variablethm
Change variablethm
ย 
AJMS_480_23.pdf
AJMS_480_23.pdfAJMS_480_23.pdf
AJMS_480_23.pdf
ย 
MT102 ะ›ะตะบั† 4
MT102 ะ›ะตะบั† 4MT102 ะ›ะตะบั† 4
MT102 ะ›ะตะบั† 4
ย 

More from Philip Schwarz

Folding Cheat Sheet #6 - sixth in a series
Folding Cheat Sheet #6 - sixth in a seriesFolding Cheat Sheet #6 - sixth in a series
Folding Cheat Sheet #6 - sixth in a series
Philip Schwarz
ย 
Folding Cheat Sheet #5 - fifth in a series
Folding Cheat Sheet #5 - fifth in a seriesFolding Cheat Sheet #5 - fifth in a series
Folding Cheat Sheet #5 - fifth in a series
Philip Schwarz
ย 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
Philip Schwarz
ย 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
ย 
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
Philip Schwarz
ย 
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
Philip Schwarz
ย 
Folding Cheat Sheet #1 - first in a series
Folding Cheat Sheet #1 - first in a seriesFolding Cheat Sheet #1 - first in a series
Folding Cheat Sheet #1 - first in a series
Philip Schwarz
ย 
Scala Left Fold Parallelisation - Three Approaches
Scala Left Fold Parallelisation- Three ApproachesScala Left Fold Parallelisation- Three Approaches
Scala Left Fold Parallelisation - Three Approaches
Philip Schwarz
ย 
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
Philip Schwarz
ย 
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
Philip Schwarz
ย 
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
Philip Schwarz
ย 
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
Philip Schwarz
ย 
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
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
ย 
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...
Philip Schwarz
ย 
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
Philip Schwarz
ย 
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...
Philip Schwarz
ย 
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...
Philip Schwarz
ย 
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
Philip Schwarz
ย 
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...
Philip Schwarz
ย 

More from Philip Schwarz (20)

Folding Cheat Sheet #6 - sixth in a series
Folding Cheat Sheet #6 - sixth in a seriesFolding Cheat Sheet #6 - sixth in a series
Folding Cheat Sheet #6 - sixth in a series
ย 
Folding Cheat Sheet #5 - fifth in a series
Folding Cheat Sheet #5 - fifth in a seriesFolding Cheat Sheet #5 - fifth in a series
Folding Cheat Sheet #5 - fifth in a series
ย 
Hand Rolled Applicative User Validation Code Kata
Hand Rolled Applicative User ValidationCode KataHand Rolled Applicative User ValidationCode Kata
Hand Rolled Applicative User Validation Code Kata
ย 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
ย 
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
ย 
Folding Cheat Sheet #1 - first in a series
Folding Cheat Sheet #1 - first in a seriesFolding Cheat Sheet #1 - first in a series
Folding Cheat Sheet #1 - first 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...
ย 
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...
ย 
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...
ย 

Recently uploaded

Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
The Third Creative Media
ย 
ๅฆ‚ไฝ•ๅŠž็†(hullๅญฆไฝ่ฏไนฆ)่‹ฑๅ›ฝ่ตซๅฐ”ๅคงๅญฆๆฏ•ไธš่ฏ็ก•ๅฃซๆ–‡ๅ‡ญๅŽŸ็‰ˆไธ€ๆจกไธ€ๆ ท
ๅฆ‚ไฝ•ๅŠž็†(hullๅญฆไฝ่ฏไนฆ)่‹ฑๅ›ฝ่ตซๅฐ”ๅคงๅญฆๆฏ•ไธš่ฏ็ก•ๅฃซๆ–‡ๅ‡ญๅŽŸ็‰ˆไธ€ๆจกไธ€ๆ ทๅฆ‚ไฝ•ๅŠž็†(hullๅญฆไฝ่ฏไนฆ)่‹ฑๅ›ฝ่ตซๅฐ”ๅคงๅญฆๆฏ•ไธš่ฏ็ก•ๅฃซๆ–‡ๅ‡ญๅŽŸ็‰ˆไธ€ๆจกไธ€ๆ ท
ๅฆ‚ไฝ•ๅŠž็†(hullๅญฆไฝ่ฏไนฆ)่‹ฑๅ›ฝ่ตซๅฐ”ๅคงๅญฆๆฏ•ไธš่ฏ็ก•ๅฃซๆ–‡ๅ‡ญๅŽŸ็‰ˆไธ€ๆจกไธ€ๆ ท
gapen1
ย 
Microsoft-Power-Platform-Adoption-Planning.pptx
Microsoft-Power-Platform-Adoption-Planning.pptxMicrosoft-Power-Platform-Adoption-Planning.pptx
Microsoft-Power-Platform-Adoption-Planning.pptx
jrodriguezq3110
ย 
Secure-by-Design Using Hardware and Software Protection for FDA Compliance
Secure-by-Design Using Hardware and Software Protection for FDA ComplianceSecure-by-Design Using Hardware and Software Protection for FDA Compliance
Secure-by-Design Using Hardware and Software Protection for FDA Compliance
ICS
ย 
Cost-Effective Strategies For iOS App Development
Cost-Effective Strategies For iOS App DevelopmentCost-Effective Strategies For iOS App Development
Cost-Effective Strategies For iOS App Development
Softradix Technologies
ย 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
Alberto Brandolini
ย 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
ย 
Boost Your Savings with These Money Management Apps
Boost Your Savings with These Money Management AppsBoost Your Savings with These Money Management Apps
Boost Your Savings with These Money Management Apps
Jhone kinadey
ย 
๐ŸŽ๏ธTech Transformation: DevOps Insights from the Experts ๐Ÿ‘ฉโ€๐Ÿ’ป
๐ŸŽ๏ธTech Transformation: DevOps Insights from the Experts ๐Ÿ‘ฉโ€๐Ÿ’ป๐ŸŽ๏ธTech Transformation: DevOps Insights from the Experts ๐Ÿ‘ฉโ€๐Ÿ’ป
๐ŸŽ๏ธTech Transformation: DevOps Insights from the Experts ๐Ÿ‘ฉโ€๐Ÿ’ป
campbellclarkson
ย 
What is Continuous Testing in DevOps - A Definitive Guide.pdf
What is Continuous Testing in DevOps - A Definitive Guide.pdfWhat is Continuous Testing in DevOps - A Definitive Guide.pdf
What is Continuous Testing in DevOps - A Definitive Guide.pdf
kalichargn70th171
ย 
Streamlining End-to-End Testing Automation
Streamlining End-to-End Testing AutomationStreamlining End-to-End Testing Automation
Streamlining End-to-End Testing Automation
Anand Bagmar
ย 
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
kalichargn70th171
ย 
ไธ€ๆฏ”ไธ€ๅŽŸ็‰ˆ(UMNๆฏ•ไธš่ฏ)ๆ˜Žๅฐผ่‹่พพๅคงๅญฆๆฏ•ไธš่ฏๅฆ‚ไฝ•ๅŠž็†
ไธ€ๆฏ”ไธ€ๅŽŸ็‰ˆ(UMNๆฏ•ไธš่ฏ)ๆ˜Žๅฐผ่‹่พพๅคงๅญฆๆฏ•ไธš่ฏๅฆ‚ไฝ•ๅŠž็†ไธ€ๆฏ”ไธ€ๅŽŸ็‰ˆ(UMNๆฏ•ไธš่ฏ)ๆ˜Žๅฐผ่‹่พพๅคงๅญฆๆฏ•ไธš่ฏๅฆ‚ไฝ•ๅŠž็†
ไธ€ๆฏ”ไธ€ๅŽŸ็‰ˆ(UMNๆฏ•ไธš่ฏ)ๆ˜Žๅฐผ่‹่พพๅคงๅญฆๆฏ•ไธš่ฏๅฆ‚ไฝ•ๅŠž็†
dakas1
ย 
Migration From CH 1.0 to CH 2.0 and Mule 4.6 & Java 17 Upgrade.pptx
Migration From CH 1.0 to CH 2.0 and  Mule 4.6 & Java 17 Upgrade.pptxMigration From CH 1.0 to CH 2.0 and  Mule 4.6 & Java 17 Upgrade.pptx
Migration From CH 1.0 to CH 2.0 and Mule 4.6 & Java 17 Upgrade.pptx
ervikas4
ย 
How GenAI Can Improve Supplier Performance Management.pdf
How GenAI Can Improve Supplier Performance Management.pdfHow GenAI Can Improve Supplier Performance Management.pdf
How GenAI Can Improve Supplier Performance Management.pdf
Zycus
ย 
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Paul Brebner
ย 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
Drona Infotech
ย 
The Comprehensive Guide to Validating Audio-Visual Performances.pdf
The Comprehensive Guide to Validating Audio-Visual Performances.pdfThe Comprehensive Guide to Validating Audio-Visual Performances.pdf
The Comprehensive Guide to Validating Audio-Visual Performances.pdf
kalichargn70th171
ย 
Ensuring Efficiency and Speed with Practical Solutions for Clinical Operations
Ensuring Efficiency and Speed with Practical Solutions for Clinical OperationsEnsuring Efficiency and Speed with Practical Solutions for Clinical Operations
Ensuring Efficiency and Speed with Practical Solutions for Clinical Operations
OnePlan Solutions
ย 

Recently uploaded (20)

Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
Unlock the Secrets to Effortless Video Creation with Invideo: Your Ultimate G...
ย 
ๅฆ‚ไฝ•ๅŠž็†(hullๅญฆไฝ่ฏไนฆ)่‹ฑๅ›ฝ่ตซๅฐ”ๅคงๅญฆๆฏ•ไธš่ฏ็ก•ๅฃซๆ–‡ๅ‡ญๅŽŸ็‰ˆไธ€ๆจกไธ€ๆ ท
ๅฆ‚ไฝ•ๅŠž็†(hullๅญฆไฝ่ฏไนฆ)่‹ฑๅ›ฝ่ตซๅฐ”ๅคงๅญฆๆฏ•ไธš่ฏ็ก•ๅฃซๆ–‡ๅ‡ญๅŽŸ็‰ˆไธ€ๆจกไธ€ๆ ทๅฆ‚ไฝ•ๅŠž็†(hullๅญฆไฝ่ฏไนฆ)่‹ฑๅ›ฝ่ตซๅฐ”ๅคงๅญฆๆฏ•ไธš่ฏ็ก•ๅฃซๆ–‡ๅ‡ญๅŽŸ็‰ˆไธ€ๆจกไธ€ๆ ท
ๅฆ‚ไฝ•ๅŠž็†(hullๅญฆไฝ่ฏไนฆ)่‹ฑๅ›ฝ่ตซๅฐ”ๅคงๅญฆๆฏ•ไธš่ฏ็ก•ๅฃซๆ–‡ๅ‡ญๅŽŸ็‰ˆไธ€ๆจกไธ€ๆ ท
ย 
Microsoft-Power-Platform-Adoption-Planning.pptx
Microsoft-Power-Platform-Adoption-Planning.pptxMicrosoft-Power-Platform-Adoption-Planning.pptx
Microsoft-Power-Platform-Adoption-Planning.pptx
ย 
Secure-by-Design Using Hardware and Software Protection for FDA Compliance
Secure-by-Design Using Hardware and Software Protection for FDA ComplianceSecure-by-Design Using Hardware and Software Protection for FDA Compliance
Secure-by-Design Using Hardware and Software Protection for FDA Compliance
ย 
Cost-Effective Strategies For iOS App Development
Cost-Effective Strategies For iOS App DevelopmentCost-Effective Strategies For iOS App Development
Cost-Effective Strategies For iOS App Development
ย 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
ย 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
ย 
Boost Your Savings with These Money Management Apps
Boost Your Savings with These Money Management AppsBoost Your Savings with These Money Management Apps
Boost Your Savings with These Money Management Apps
ย 
๐ŸŽ๏ธTech Transformation: DevOps Insights from the Experts ๐Ÿ‘ฉโ€๐Ÿ’ป
๐ŸŽ๏ธTech Transformation: DevOps Insights from the Experts ๐Ÿ‘ฉโ€๐Ÿ’ป๐ŸŽ๏ธTech Transformation: DevOps Insights from the Experts ๐Ÿ‘ฉโ€๐Ÿ’ป
๐ŸŽ๏ธTech Transformation: DevOps Insights from the Experts ๐Ÿ‘ฉโ€๐Ÿ’ป
ย 
What is Continuous Testing in DevOps - A Definitive Guide.pdf
What is Continuous Testing in DevOps - A Definitive Guide.pdfWhat is Continuous Testing in DevOps - A Definitive Guide.pdf
What is Continuous Testing in DevOps - A Definitive Guide.pdf
ย 
Streamlining End-to-End Testing Automation
Streamlining End-to-End Testing AutomationStreamlining End-to-End Testing Automation
Streamlining End-to-End Testing Automation
ย 
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
The Power of Visual Regression Testing_ Why It Is Critical for Enterprise App...
ย 
ไธ€ๆฏ”ไธ€ๅŽŸ็‰ˆ(UMNๆฏ•ไธš่ฏ)ๆ˜Žๅฐผ่‹่พพๅคงๅญฆๆฏ•ไธš่ฏๅฆ‚ไฝ•ๅŠž็†
ไธ€ๆฏ”ไธ€ๅŽŸ็‰ˆ(UMNๆฏ•ไธš่ฏ)ๆ˜Žๅฐผ่‹่พพๅคงๅญฆๆฏ•ไธš่ฏๅฆ‚ไฝ•ๅŠž็†ไธ€ๆฏ”ไธ€ๅŽŸ็‰ˆ(UMNๆฏ•ไธš่ฏ)ๆ˜Žๅฐผ่‹่พพๅคงๅญฆๆฏ•ไธš่ฏๅฆ‚ไฝ•ๅŠž็†
ไธ€ๆฏ”ไธ€ๅŽŸ็‰ˆ(UMNๆฏ•ไธš่ฏ)ๆ˜Žๅฐผ่‹่พพๅคงๅญฆๆฏ•ไธš่ฏๅฆ‚ไฝ•ๅŠž็†
ย 
Migration From CH 1.0 to CH 2.0 and Mule 4.6 & Java 17 Upgrade.pptx
Migration From CH 1.0 to CH 2.0 and  Mule 4.6 & Java 17 Upgrade.pptxMigration From CH 1.0 to CH 2.0 and  Mule 4.6 & Java 17 Upgrade.pptx
Migration From CH 1.0 to CH 2.0 and Mule 4.6 & Java 17 Upgrade.pptx
ย 
How GenAI Can Improve Supplier Performance Management.pdf
How GenAI Can Improve Supplier Performance Management.pdfHow GenAI Can Improve Supplier Performance Management.pdf
How GenAI Can Improve Supplier Performance Management.pdf
ย 
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
ย 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
ย 
The Comprehensive Guide to Validating Audio-Visual Performances.pdf
The Comprehensive Guide to Validating Audio-Visual Performances.pdfThe Comprehensive Guide to Validating Audio-Visual Performances.pdf
The Comprehensive Guide to Validating Audio-Visual Performances.pdf
ย 
bgiolcb
bgiolcbbgiolcb
bgiolcb
ย 
Ensuring Efficiency and Speed with Practical Solutions for Clinical Operations
Ensuring Efficiency and Speed with Practical Solutions for Clinical OperationsEnsuring Efficiency and Speed with Practical Solutions for Clinical Operations
Ensuring Efficiency and Speed with Practical Solutions for Clinical Operations
ย 

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

  • 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.