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.

Unifying fold left and fold right in Prolog

90 pointsby gopiandcodeover 2 years ago

4 comments

jfarmerover 2 years ago
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 未加载
rravalover 2 years ago
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.
galaxyLogicover 2 years ago
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 未加载
hddqsbover 2 years ago
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 未加载