Here is another solution to search-generator. Below, the call to yield and the outermost call/cc could actually be removed without affecting the result. Let's assume we do so. As in the original search, the body of the (pred? ...) clause will return L as the result of (search (car L)), which short-circuits the OR and exits the function immediately with that value. When resumed, #f is returned to the OR clause and the search resumes with (search (cdr L)). This is a special case which relies on the fact that we are not using recursion, and can short-circuit with OR; in general you can't omit the yield.
<pre>
(define (search-generator L pred?)
(let ((resume #f))
(lambda ()
(call/cc
(lambda (yield)
(if resume
(resume #f)
(let search ((L L))
(cond ((null? L) #f)
((pair? L) (or (search (car L))
(search (cdr L))))
((pred? L)
(call/cc (lambda (k)
(set! resume k)
(yield L))))
(else #f)))))))))
</pre>
↧