Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>From the Racket <a href="http://docs.racket-lang.org/reference/pairs.html" rel="nofollow">documentation</a>, the description of <code>foldl</code>:</p> <pre><code>(foldl proc init lst ...+) → any/c </code></pre> <p>Two points of interest for your question are mentioned:</p> <blockquote> <p>the input lsts are traversed from left to right</p> </blockquote> <p>And</p> <blockquote> <p>foldl processes the lsts in constant space</p> </blockquote> <p>I'm gonna speculate on how the implementation for that might look like, with a single list for simplicity's sake:</p> <pre><code>(define (my-foldl proc init lst) (define (iter lst acc) (if (null? lst) acc (iter (cdr lst) (proc (car lst) acc)))) (iter lst init)) </code></pre> <p>As you can see, the requirements of left-to-right traversal and constant space are met (notice the tail recursion in <code>iter</code>), but the <em>order</em> of the arguments for <code>proc</code> was never specified in the description. Hence, the result of calling the above code would be:</p> <pre><code>(my-foldl - 0 '(1 2 3 4)) &gt; 2 </code></pre> <p>If we had specified the order of the arguments for <code>proc</code> in this way:</p> <pre><code>(proc acc (car lst)) </code></pre> <p>Then the result would be:</p> <pre><code>(my-foldl - 0 '(1 2 3 4)) &gt; -10 </code></pre> <p>My point is, the documentation for <code>foldl</code> doesn't make any assumptions on the evaluation order of the arguments for <code>proc</code>, it only has to guarantee that constant space is used and that the elements in the list are evaluated from left to right.</p> <p>As a side note, you can get the desired evaluation order for your expression by simply writing this:</p> <pre><code>(- 0 1 2 3 4) &gt; -10 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload