Please take a look at two-in-a-row*?
function in chapter 19.
My question is about the (leave '())
in the get-first
helper function. Note that (waddle l)
will either return '()
or an atom, which indicates the list has exhausted or an atom from the list is retrieved.
Without (leave '())
it will still return those two kinds of values, just not use the continuation leave
. But the book says without (leave '())
is bad, I just can not see why.
(define two-in-a-row*
(letrec ([leave id] ; the identity function
[fill id]
[waddle (lambda (l)
(cond [(null? l) '()]
[(atom? (car l))
(begin
(letcc rest
(set! fill rest)
(leave (car l)))
(waddle (cdr l)))]
[else
(begin
(waddle (car l))
(waddle (cdr l)))]))]
[get-first (lambda (l)
(letcc here
(set! leave here)
(waddle l)
(leave '()) ; why is this part needed???
))]
[get-next (lambda (l)
(letcc here
(set! leave here)
(fill 'go)))]
[T? (lambda (a)
(let ([n (get-next 'dummy)])
(if (atom? n)
(or (eq? a n)
(T? n))
#f)))])
(lambda (l)
(let ([fst (get-first l)])
(if (atom? fst)
(T? fst)
#f)))))
Thanks very much.
Another interesting tread about this problem.