In this lecture on Haskell programming, there is a fold implementation defined as follows:
fold :: (a -> b -> b) -> b -> [a] -> b
fold f z [] = z
fold f z (x:xs) = f x (fold z f xs)
The idea is to use it to determine the amount, product, etc.
sum'' = fold (+) 0
product'' = fold (*) 1
length'' = fold addOne 0
where addOne _ s = 1 + s
There seems to be an inversion between zand fwithin the recursion pattern: otherwise, how does it z f xsfit (a -> b -> b) -> b -> [a]?
In my opinion, the recursion pattern should be
fold f z (x:xs) = f x (fold f z xs)
However, soon in the lecture you can find the following statement:
fold f z [a,b,c] = a `f` (b `f` (c `f` z))
This reinforces the so-called mistake, so I suppose there should be a mistake in my head!
Doesn't that look like the following?
fold f z [a,b,c] = `f` a (`f` b (`f` c z))
Am I missing a point, or is it a double mistake in a lecture?