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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Unifying fold left and fold right in Prolog

90 点作者 gopiandcode超过 2 年前

4 条评论

jfarmer超过 2 年前
Here&#x27;s a neat paper on fold by Graham Hutton: &quot;A tutorial on the universality and expressiveness of fold&quot;<p><a href="https:&#x2F;&#x2F;www.cs.nott.ac.uk&#x2F;~pszgmh&#x2F;fold.pdf" rel="nofollow">https:&#x2F;&#x2F;www.cs.nott.ac.uk&#x2F;~pszgmh&#x2F;fold.pdf</a><p>It gives a sufficient and necessary condition for when a function can be written as a (right) fold.<p>It&#x27;s not clear whether the author means to say foldl and foldr are equivalent, or only that any foldl call which terminates can be converted into a foldr call.<p>The latter is true, but the former is false.<p>For finite lists, every foldl call can be translated into a foldr call that produces the same result (and vice versa).<p>Foldr still makes sense for infinite lists in a language w&#x2F; lazy evaluation, but foldl will recurse forever.
评论 #32610889 未加载
评论 #32610723 未加载
rraval超过 2 年前
Related: Graham Hutton&#x27;s classic paper: a tutorial on the universality and expressiveness of fold<p>[PDF]: <a href="https:&#x2F;&#x2F;www.cs.nott.ac.uk&#x2F;~pszgmh&#x2F;fold.pdf" rel="nofollow">https:&#x2F;&#x2F;www.cs.nott.ac.uk&#x2F;~pszgmh&#x2F;fold.pdf</a><p>Section 5.1 walks the reader through implementing foldl in terms of foldr after laying down the ground work and gradually introducing things one concept at a time.<p>For me, the eye-opening insight was using foldr to generate intermediate functions as values, which is the sort of thing &quot;functional&quot; programming does without thinking twice, but is mind bending for someone coming from a more traditional procedural language background.
galaxyLogic超过 2 年前
Paper-based explanation of folding:<p><a href="https:&#x2F;&#x2F;www.globalnerdy.com&#x2F;2008&#x2F;09&#x2F;03&#x2F;enumerating-enumerable-a-cute-trick-for-explaining-inject-reduce-fold&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.globalnerdy.com&#x2F;2008&#x2F;09&#x2F;03&#x2F;enumerating-enumerabl...</a>
评论 #32612516 未加载
hddqsb超过 2 年前
The author is confused, their Prolog implementation is clearly a left fold!<p>I&#x27;m only half joking :)<p>To elaborate: The variable names &quot;ACC&quot; and &quot;RES&quot; suggest the predicate should be viewed as a left fold, because when the predicate is viewed as a right fold the meanings of the variables are flipped.
评论 #32634614 未加载