foldilocks
foldilocks copied to clipboard
sumFold example could be clearer with foldr
I wonder if the sumFold example would be clearer if it were written using foldr instead of with foldl. In particular, because sumManual is written with the same kind of recursion as foldr, this would be easier to see:
Turns out, the definition of foldl contains all of that sweet recursive machinery needed to power our manual sum function:
foldr f z [] = z
foldr f z (x:xs) = x `f` foldr f z xs
sumManual [] = 0
sumManual (x:xs) = x + sumManual xs
See how well they line up?
(I really enjoy how this is written overall.)