【发布时间】:2017-06-22 01:06:57
【问题描述】:
请查看第 19 章中的 two-in-a-row*? 函数。
我的问题是关于 get-first 辅助函数中的 (leave '())。请注意,(waddle l) 将返回 '() 或原子,这表明列表已用尽或检索到列表中的原子。
如果没有(leave '()),它仍然会返回这两种值,只是不要使用延续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)))))
非常感谢。
关于这个问题的另一个有趣的tread。
【问题讨论】:
标签: scheme continuations callcc seasoned-schemer