Trying to learn F# but got confused when trying to distinguish between fold and reduce. Fold seems to do the same thing but takes an extra parameter. Is there a legitimate reason for these two functions to exist or they are there to accommodate people with different backgrounds? (E.g.: String and string in C#)
Here is code snippet copied from sample:
let sumAList list =
List.reduce (fun acc elem -> acc + elem) list
let sumAFoldingList list =
List.fold (fun acc elem -> acc + elem) 0 list
printfn "Are these two the same? %A "
(sumAList [2; 4; 10] = sumAFoldingList [2; 4; 10])
fold f a l
can be written asreduce f a::l
. – Neilfold
in terms ofreduce
is more complicated than that - the type of accumulator offold
does not have to be the same as the type of things in the list! – Tomas Petricek