سؤال

The seasoned schemer on page 78 has the below definition of leftmost and lm.

(define leftmost
  (lambda (l)
    (letcc skip
      (lm l skip))))

(define lm
  (lambda (l out)
    (cond
      ((null? l) (quote ()))
      ((atom? (car l)) (out (car l)))
      (else (let ()
              (lm (car l) out)
              (lm (cdr l) out))))))

On the next page it has the following explanation of having multiple expressions in the value part. I don't understand the explanation of how it makes leftmost work on, for example (() a).

When a (let ...) has two expressions in its value part, we must first determine the value of the first expression. If it has one, we ignore it and determine the value of the second expression."

هل كانت مفيدة؟

المحلول

The key to this is that out in this case is a continuation. Continuations, unlike procedures, do not return once invoked.

So, the two expressions in let are executed sequentially: first, (lm (car l) out), then (lm (cdr l) out). Because out is a continuation, and it's invoked when an atom is encountered, the (lm (car l) out) will not return if an atom is encountered in the (car l). So the (lm (cdr l) out) will only happen if there are no atoms in (car l)---which is the case for your example of (() a).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top