TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Reduce vs. Fold in Common Lisp

92 点作者 billiob将近 2 年前

8 条评论

ruricolist将近 2 年前
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 未加载
varjag将近 2 年前
<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 未加载
somewhereoutth将近 2 年前
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.
dreamcompiler将近 2 年前
&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.
tangus将近 2 年前
&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 未加载
User23将近 2 年前
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 未加载
lispm将近 2 年前
so foldl is basically<p><pre><code> (defun foldl (function value sequence) (reduce function sequence :initial-value value))</code></pre>
评论 #36192766 未加载
mgaunard将近 2 年前
+ is not associative for many types.
评论 #36193818 未加载