TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Reduce vs. Fold in Common Lisp

92 pointsby billiobalmost 2 years ago

8 comments

ruricolistalmost 2 years ago
The history here is that Common Lisp gets reduce from APL (<a href="https:&#x2F;&#x2F;aplwiki.com&#x2F;wiki&#x2F;Reduce" rel="nofollow">https:&#x2F;&#x2F;aplwiki.com&#x2F;wiki&#x2F;Reduce</a>). It&#x27;s not an attempt an ML-style fold, but a different formalism from a different lineage.
评论 #36191611 未加载
评论 #36191540 未加载
varjagalmost 2 years ago
<i>…REDUCE functions can be called with zero, one or two list values.</i><p>No, it&#x27;s either two or zero arguments.
评论 #36190209 未加载
somewhereoutthalmost 2 years ago
fold is a natural consequence of church lists, probably the most fundamental way of expressing the list abstraction:<p>\f \x (f a0 (f a1 (f a2 x)))<p>So fold is just applying a list (function) to 2 arguments. Or you can be helpful and make something like fold := \f \x \l l f x which is useful for binding the f and the x and applying to multiple lists (everything is Curried of course)<p>LISP is not quite based on lambda calculus, so it should be no surprise it doesn&#x27;t quite get reduce(i.e. fold) right.<p>See also church numerals, which are like lists but without the elements, they also have a &#x27;fold&#x27;:<p>\f \x f (f (f x))) == 3<p>We can make trees! Which again also have a &#x27;fold&#x27;<p>\f \x f a0 (x a1) (f a2 (x a3) (x a4))<p>And many other more exotic folding data structures.
dreamcompileralmost 2 years ago
&gt; It then applies the function to successive pairs of sequence elements.<p>No. #&#x27;reduce may take the first pair as an optimization step, but from that point on it processes sequence elements one at a time. It passes an accumulated value and the next sequence value to the function.
tangusalmost 2 years ago
&gt;Fold is also simpler than REDUCE since it does not have any special case, making it easier to reason about its behaviour.<p>If returning the initial value when the list is empty is considered a special case (or &quot;surprising aspect&quot;) of REDUCE, then it&#x27;s the same for FOLD, no?
评论 #36190443 未加载
评论 #36190405 未加载
User23almost 2 years ago
I don&#x27;t have a running image handy, but<p><pre><code> (+) ; =&gt; 0 (*) ; =&gt; 1 </code></pre> and<p><pre><code> (+ n) ; =&gt; n (* n) ; =&gt; n </code></pre> which I expect has some bearing on the behavior of reduce in the examples given.<p>It&#x27;s pretty obvious that any other function could either have or be advised to have whatever equivalent semantics are appropriate.<p>Of course<p><pre><code> (apply #&#x27;+ &#x27;(1 2 3 4 5)) ; =&gt; 15 </code></pre> So reduce can be obviated by just letting the function take variable args too.
评论 #36193010 未加载
lispmalmost 2 years ago
so foldl is basically<p><pre><code> (defun foldl (function value sequence) (reduce function sequence :initial-value value))</code></pre>
评论 #36192766 未加载
mgaunardalmost 2 years ago
+ is not associative for many types.
评论 #36193818 未加载