I\'m trying to understand a part in the lecture notes of a class I\'m taking. It defines the length function as:
length = foldr (\\_ n -> 1 + n) 0
The function foldr
is to fold the list with a right associative operator, you can easily understand what the function does if you use the operator(+)
, (The function has the same behavior as sum
):
foldr (+) 0 [1,2,3,4,5] = 1+(2+(3+(4+(5+0))))
For your length function, it is equivalent to:
foldr (\_ n -> 1 + n) 0 [1,2,3,4,5] = 1+(1+(1+(1+(1+0))))
That is what the foldr
for