Fold (higher-order function): Difference between revisions

Content deleted Content added
Glv (talk | contribs)
m fix broken link to clojure.core.reducers/fold docs
Examples: add remarkably short code for finite primes
Line 130:
where the function [[Haskell features#union|<code>union</code>]] operates on ordered lists in a local manner to efficiently produce their [[Union (set theory)|set union]], and [[Haskell features#minus|<code>minus</code>]] their [[Complement (set theory)#Relative complement|set difference]].
 
A finite prefix of primes is concisely defined as a folding of set difference operation over the lists of enumerated multiples of integers, as
<syntaxhighlight lang="haskell">
primesTo n = foldl1 minus [[2*x,3*x..n] | x <- [1..n]]
</syntaxhighlight>
For finite lists, e.g., [[merge sort]] (and its duplicates-removing variety, <code>nubsort</code>) could be easily defined using tree-like folding as
<syntaxhighlight lang="haskell">