In haskell, the initial value for the foldl operator is clearly mandatory
Prelude> foldl (+) 0 [1]
1
Prelude> foldl (+) 0 []
0
Prelude> :t foldl
(a -> b -> a) -> a -> [b] -> a
But in the reduce function (or functools.reduce), the initial value is optional
reduce(function, sequence[, initial]) -> value
The only time the initial value is required is when the sequence is empty. That is congruent with the behaviour in haskell. Does reduce in python assumes that if the sequence is of size 1; then it handles these corner cases internally ?
>> reduce(operator.sub, [1])
1
>> reduce(operator.mul, [1])
1
>> reduce(operator.add, [1])
1
From the manual:
I also want to comment on this:
This is not entirely true. In a fold operation you usually always assume that you have an earlier result to work with. If your fold operation is appending elements to a list the initial element can be the empty list, so you can add elements to that. More generally, the initial value is always needed when the return type of the function differs from the type of the elements in the list.