经验丰富的Schemer的get-first,get-next和waddle函数 [英] Seasoned Schemer's get-first, get-next, and waddle functions

查看:98
本文介绍了经验丰富的Schemer的get-first,get-next和waddle函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(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))))))

对于不熟悉《经验丰富的计划者》一书的任何人,get-firstget-nextwaddle(此处未定义最后两个)都是显然可以对协程进行建模的过程.通过传递给waddle tree 仅产生叶子.在waddle倒数第二次重新输入的收益率之前,它将重新输入点设置为仅返回纯值'()的位置,即不产生'(),即<waddle 的em>实际值'(),好像它一直都是纯函数一样.

牢记这一点,我们可以看到get-first的设置...当waddle返回"for real"时,它将位于get-first中的call/cc内部,然后(leave (quote ()))是值get-first的值(进而,该leave打算在上一次迭代中返回到get-next,因此,get-next进行'()的实际"返回).

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

解决方案

之所以困惑,是因为"leave"不是我想要的功能,而是它在何时求值的功能.将对其进行评估,该评估似乎是从左到右,因此在"waddle"之前.这意味着它将评估之前语句中的设置.

道德:当使用需要在函数调用内重新定义的函数时要当心!如果在从右到左的解释器上进行操作,则将在查找符号leave之前将其评估为waddle,该函数释放"到任何地方,在这段时间内它将被设置为DIFFERENT函数./p>

(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))))))

For anybody not familiar with the book "The Seasoned Schemer", get-first, get-next, and waddle (last two not defined here) are procedures to apparently model coroutines to iterate through a tree passed to waddle that yields leaves only. Just prior to waddle's yield on its second-to-last re-entry, it sets the re-entry point to where it will only ever return the pure value '() i.e. instead of yielding '() , the actual value of waddle is '() , as if it were a pure function all along.

With this in mind, we can see what get-first sets up... When waddle returns "for real", it will be inside the call/cc in get-first and then (leave (quote ())) is the value of get-first (and, in turn, this leave is intended to return to get-next on the last iteration, therefore it is get-next that does the "actual" return of '()).

So why is the second version not equivalent, where waddle's value of '() would be the argument to leave?

解决方案

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.

这篇关于经验丰富的Schemer的get-first,get-next和waddle函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆