Programing Language: Fold vs Reduce
fold vs reduce
in programing languages,
- is fold and reduce the same thing but just different name?
- what's the history of fold and reduce?
- grok ai answers
- https://x.com/i/grok/share/uTzwazML2Qu5hQluw87e0PJIB
- mega debate on whether fold and reduce in functional programing languages are the same, and their origin and history in computer science.
- big debate since 5 months ago in 2025-05 on xah discord.
- after massive research by all, here's summary.
xah is correct.
Origin of the concept of fold or reduce
- Fold n reduce, r the same thing conceptionally.
- Origin in lambda calculus and recursion theory (aka computability theory)
- Fold is the function form of loop.
- Some programing language name it fold, some reduce.
- Later on, in programing languages, it started to diversify.
- Reduce became a version of fold without the init value.
- While fold, has init.
- Again, langs differ. Among those that make them seperate functions, they do not necessarily have the same meaning of their fold and reduce.
Why do some programing language have both fold and reduce and are different?
The reason fold and reduce diversified is because, its easier to have separate functions to deal with optional parameter of the init value, or special case of empty list. e.g. in fsharp, in guile scheme lisp. And in Cpp, it is far worse.
Typically, for those programing language that have separate fold and reduce, the reduce is the one that just takes a list, including if the list is empty, and no init value.
In Science of Terminology and Communication, Which Term is Better? Fold or Reduce?
- Jargon “fold” is better, because it connotes a structural operation, that more conveys the essence of loop in functional form.
- Jargon “reduce” just convey reducing a list into a single value.
- Popularity of “reduce” is spurred by the Cpp imperative bit diddling dolts. They think of reduce as reducing a list or array into a single value.
What is the Relation Reduce to Parallel Computing
- What about the parallel issue?
- Basically, it is a unrelated issue.
- Fold by its essense as functional form of loop, require sequential operation.
- The parallel computation optimization issue, works when the operation has associative and commutative properties.
- And basically no pop programing language can specify an operation or function to have associative or commutative properties. (it is inpractical for arbitrary functions. Undecidable.)
- In general, when a func has those properties, you call a function named parallel(op, list) to compute.
What about Google's Map Reduce? does it mean reduce is related to parallel computation?
- no.
- Google's Map Reduce is a marketing term for their product.
- it is not computer science.
- it uses the jargon Map, and the jargon Reduce, because they are dramatic, associated with exotic functional programing.
What is commutative property and associative property
commutative property means
f(a,b) == f(b,a)
associative property means
f(f(a,b),c) == f(a,f(b,c))
what operations have commutative property and associative property?
example plus.
- plus(a,b) == plus(b,a)
- plus(plus(a,b),c) == plus(a,plus(b,c))
can be also written as
- a+b == b+a
- (a+b)+c == a+(b+c)
also, multiplication too.
most operation (aka function) are not commutative, and not associative property.
- For example minus.
- or, For example , matrix multiplication.