请看two-in-a-row*?
第 19 章中的函数。
我的问题是关于辅助函数(leave '())
中的。get-first
请注意,(waddle l)
将返回'()
或返回原子,这表明列表已用尽或检索到列表中的原子。
没有(leave '())
它仍然会返回这两种值,只是不使用 continuation leave
。但是书上说没有(leave '())
就是不好的,我就是不明白为什么。
(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)))))
非常感谢。
关于这个问题的另一个有趣的步骤。