5
(define get-first
  (lambda (l)
    (call-with-current-continuation
      (lambda (here)
        (set! leave here)
        (waddle l)
        (leave (quote ()))))))

(define get-first
  (lambda (l)
    (call-with-current-continuation
      (lambda (here)
        (set! leave here)
        (leave (waddle l))))))

对于任何不熟悉“The Seasoned Schemer”一书的人来说get-firstget-next、 和waddle(最后两个未在此处定义)显然是对协程建模以遍历传递给仅产生叶子的树的过程。waddle就在waddle倒数第二次重新进入的 y​​ield 之前,它将重新进入点设置为它只会返回纯值的位置,即is实际值'()不是yield ,就好像它是一直都是纯函数。 '()waddle '()

考虑到这一点,我们可以看到get-first设置了什么......当waddle返回“真实”时,它将在call/ccin内部get-first,然后(leave (quote ()))get-first(并且,反过来,这leave旨在返回到get-next最后一次迭代,因此,它是)get-next的“实际”回报'()

那么为什么第二个版本不等价,其中waddle的值'()将是 的参数leave

4

1 回答 1

4

The confusion is because "leave" is not the function I want it to be, but the function it evaluates to when it's evaluated, which appears to be left-to-right and thus before "waddle". That means it evaluates to what it was just set to, in the statement prior.

Moral: beware when using functions that are subject to redefining WITHIN the call to the function! If this was on a right-to-left interpreter, waddle would be evaluated before the symbol leave was looked up as the function that "leaves" to wherever, during which time it would be set to a DIFFERENT function.

于 2012-06-19T03:42:58.740 回答