Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Let's step through an example; maybe that will help. :-) For simplicity, I'm just going to use <code>list</code> as the collector/continuation, which will just return a list with the arguments to the continuation.</p> <pre><code>(multirember&amp;co 'foo '(foo bar) list) </code></pre> <p>At the start,</p> <pre><code>a = 'foo lat = '(foo bar) col = list </code></pre> <p>At the first iteration, the <code>(eq? (car lat) a)</code> condition matches, since <code>lat</code> is not empty, and the first element of <code>lat</code> is <code>'foo</code>. This sets up the next recursion to <code>multirember&amp;co</code> thusly:</p> <pre><code>a = 'foo lat = '(bar) col = (lambda (newlat seen) (list newlat (cons 'foo seen)) </code></pre> <p>At the next iteration, the <code>else</code> matches: since <code>lat</code> is not empty, and the first element of <code>lat</code> is <code>'bar</code> (and not <code>'foo</code>). Thus, for the next recursion, we then have:</p> <pre><code>a = 'foo lat = '() col = (lambda (newlat seen) ((lambda (newlat seen) (list newlat (cons 'foo seen))) (cons 'bar newlat) seen)) </code></pre> <p>For ease of human reading (and avoid confusion), we can rename the parameters (due to lexical scoping), without any change to the program's semantics:</p> <pre><code>col = (lambda (newlat1 seen1) ((lambda (newlat2 seen2) (list newlat2 (cons 'foo seen2))) (cons 'bar newlat1) seen1)) </code></pre> <p>Finally, the <code>(null? lat)</code> clause matches, since <code>lat</code> is now empty. So we call</p> <pre><code>(col '() '()) </code></pre> <p>which expands to:</p> <pre><code>((lambda (newlat1 seen1) ((lambda (newlat2 seen2) (list newlat2 (cons 'foo seen2))) (cons 'bar newlat1) seen1)) '() '()) </code></pre> <p>which (when substituting <code>newlat1 = '()</code> and <code>seen1 = '()</code>) becomes</p> <pre><code>((lambda (newlat2 seen2) (list newlat2 (cons 'foo seen2))) (cons 'bar '()) '()) </code></pre> <p>or (evaluating <code>(cons 'bar '())</code>)</p> <pre><code>((lambda (newlat2 seen2) (list newlat2 (cons 'foo seen2))) '(bar) '()) </code></pre> <p>Now, substituting the values <code>newlat2 = '(bar)</code> and <code>seen2 = '()</code>, we get</p> <pre><code>(list '(bar) (cons 'foo '())) </code></pre> <p>or, in other words,</p> <pre><code>(list '(bar) '(foo)) </code></pre> <p>to give our final result of</p> <pre><code>'((bar) (foo)) </code></pre>
 

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