Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>We could use <strong>equal?</strong></p> <pre><code> (equal? '(a (b (c))) '(a (b (c)))) </code></pre> <p>But, for some fun, following on from Vassermans mention of a "break", this might be a good chance to take advantage of Schemes continuation controlling power!</p> <p>We can use <strong>call/cc</strong> to issue an early return if we notice any difference in the trees. This way we can just jump back to the callers continuation without having to unwind the stack.</p> <p>Here is a really simple example. It assumes the trees are well-formed and only contain symbols as leaves, but it should hopefully demonstrate the concept. You'll see that the procedure explicitly accepts the continuation as a parameter.</p> <pre><code> (define (same? a b return) (cond ((and (symbol? a) (symbol? b)) ; Both Symbols. Make sure they are the same. (if (not (eq? a b)) (return #f))) ((and (empty? a) (empty? b))) ; Both are empty, so far so good. ((not (eq? (empty? a) (empty? b))) ; One tree is empty, must be different! (return #f)) (else (begin (same? (car a) (car b) return) ; Lets keep on looking. (same? (cdr a) (cdr b) return))))) </code></pre> <p><strong>call/cc</strong> lets us capture the current continuation. Here is how I called this procedure:</p> <pre><code> (call/cc (lambda (k) (same? '(a (b)) '(a (b)) k))) ; --&gt; #t (call/cc (lambda (k) (same? '(a (b (c) (d e))) '(a (b (c) (d e))) k))) ; --&gt; #t (call/cc (lambda (k) (same? '(a (b (F) (d e))) '(a (b (c) (d e))) k))) ; --&gt; #f (call/cc (lambda (k) (same? '(a (b)) '(a (b (c) (d))) k))) ; --&gt; #f </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