长度≤1如何多次执行? [英] How to do this length≤1 more than once?

查看:106
本文介绍了长度≤1如何多次执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了一天的时间阅读《小策划者》一书中第166页的length≤1;有以下代码:

I've spent a day reading page 166's length≤1 in the book The Little Schemer; there's the following code:

(((lambda (mk-length)
    (mk-length mk-length))
  (lambda (mk-length)
   (lambda (l)
    (cond
      ((null? l) 0)
      (else (add1 
           ((mk-length eternity)
            (cdr l))))))))
 l)

其中l(apples)eternity如下:

(define eternity 
  (lambda (x)
    (eternity x)))

第166页(第四版)指出:

Page 166 (4th ed.) states that:

当我们一次应用mk-length时,我们得到length≤1

然后

我们可以多次这样做吗?

Could we do this more than once?

但是我不知道如何获取length≤2?

But I do not know how to do this for getting length≤2?

推荐答案

假定l(apples oranges),则它将像这样进行评估(请注意,mk-length绑定到(lambda (mk-length) ...)函数本身:

Suppose l is (apples oranges), then it will evaluate like this (note that mk-length is bound to the the (lambda (mk-length) ...) function itself:

(cond ((null? l) 0) 
      (else (add1 ((mk-length eternity) (cdr l)))))
==>
(add1 ((mk-length eternity) '(oranges)))
==>
(add1 ((lambda (l) (cond ((null? l) 0
                          (else (add1 ((eternity eternity) (cdr l))))))))
==>
(add1 (add1 ((eternity eternity) '())))

因此,在这里,经过两个步骤,最终应用了eternity,但是我们要调用的是mk-length.因此,在原始函数中,如果将eternity替换为mk-length,则我编写的最后一步将包含(mk-length mk-length)而不是(eternity eternity),从而使计算得以进行.

So here, after two steps, eternity ends up being applied, but what we want is for it to call mk-length. So in the original function, if we replace eternity by mk-length, then the last step I wrote will contain (mk-length mk-length) instead of (eternity eternity), allowing the computation to proceed.

这篇关于长度≤1如何多次执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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