re: Understanding the Fold Operation VIEW POST

VIEW FULL DISCUSSION
 
 

I haven't tracked the history, but my understanding from ML is that fold starts with an initial value. Whereas reduce uses the first item of the collection as the initial value. Only problem is, reduce doesn't work when the list is empty, but fold does.

 

fold starts with an initial value, whereas reduce uses the first item

Nah, mind to name a single modern language where reduce does not accept initial value? Historially—yes, but nowadays this argument sounds as “fold has foldr version that folds in the reverse order.” C’mon, just reverse and then reduce :)

reduce doesn't work when the list is empty, but fold does

In what language? The whole dozen I am aware of does.

In OCaml and F#, List.reduce and List.fold behave as I describe. In F#, List.reduce throws when given an empty list. In OCaml it returns None or Some for a non-empty list.

code of conduct - report abuse