For a presentation I&#39;m working on I&#39;ve been implementing various higher order functions in javascript.<div><br></div><div>When trying to work out the proper foldl implementation I came across something interesting in Racket</div>
<div><br></div><div><div>&gt; (foldl cons &#39;() &#39;(1 2 3 4))</div><div>(4 3 2 1)</div><div><br></div><div>&gt; (foldl - 0 &#39;(1 2 3 4))</div><div>2</div></div><div><br></div><div>However when I looked into various other implementations of foldl I came across very different behavior</div>
<div>Haskell&#39;s foldl seems to be implemented like this:</div><div><div>;;haskell-like definition</div><div>(define (foldl-haskell func accum lst)</div><div>  (if (null? lst)</div><div>      accum</div><div>      (foldl-haskell func (func accum (car lst)) (cdr lst))))</div>
</div><div><br></div><div>and yields these results:</div><div><div>&gt; (foldl-haskell cons &#39;() &#39;(1 2 3 4))</div><div>((((() . 1) . 2) . 3) . 4)</div><div>&gt; (foldl-haskell - 0 &#39;(1 2 3 4))</div><div>-10</div>
<div>&gt; </div></div><div><br></div><div>Unsure which was correct I broke out SICP which uses this definition:</div><div><div>;;sicp version</div><div>(define (fold-left op initial sequence)</div><div>  (define (iter result rest)</div>
<div>    (if (null? rest)</div><div>        result</div><div>        (iter (op result (car rest))</div><div>              (cdr rest))))</div><div>  (iter initial sequence))</div></div><div><br></div><div>with these results (same as the haskell-like version):</div>
<div><div>&gt; (fold-left cons &#39;() &#39;(1 2 3 4))</div><div>((((() . 1) . 2) . 3) . 4)</div><div>&gt; (fold-left - 0 &#39;(1 2 3 4))</div><div>-10</div><div>&gt; </div></div><div><br></div><div>So which of these is the canonical implementation of a left fold? Why the difference in Racket? </div>
<div>For pure aesthetics I like the behavior of Racket in the case of cons, but for &#39;-&#39; the others seems to make more sense.</div><div><br></div><div>Any insight into this is greatly appreciated!</div><div><br></div>
<div>Thanks!</div><div>--Will Kurt</div>