小计划者:length0和mk-length [英] Little Schemer: length0 and mk-length

查看:117
本文介绍了小计划者:length0和mk-length的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

小策划人在第165页,仍然是功能 length 0 .但是,这是如何工作的呢?似乎将 length lambda 传递给了 mk-length lambda ,后者使用 length lambda 本身作为参数传递.因此,当计算底部的(length (cdr l))时,length就是 length lambda 本身.但是 length lambda 带有两个可咖喱的参数:lengthl.那么(length (cdr l))怎么可能呢?

The little schemer gives the following on page 165 as still the function length0. But how does this work? It looks like the length lambda is being passed to the mk-length lambda, which evaluates the length lambda with the length lambda itself passed as an argument. So then, when (length (cdr l)) at the bottom is evaluated length is just the length lambda itself. But the length lambda takes two parameters curried: length and l. So how can (length (cdr l)) make sense then?

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

推荐答案

小计划者正在建立一个函数,该函数适用于长度越来越长的列表.长度≤0的部分原因在于,它仅适用于长度小于或等于零的列表(并且由于没有负长度的列表,因此意味着长度为零的列表).您故意显示的代码 仅适用于长度为零的列表.实际上,甚至在

The Little Schemer is building up a function that will work for lists of greater and greater lengths. Part of the point of length≤0 is that it only works for lists whose length is less than or equal to zero (and since there are no lists of negative length, this means lists of length zero). The code that you've shown intentionally only works for lists of length zero. In fact, this is even pointed out in the text on page 165:

A:此时,如果我们可以为 eternity 创建另一个 mk-length 的应用程序怎么办?
B:那只会将问题推迟到一个,而且我们该怎么做?
A:好吧,由于没有人关心我们传递给 mk-length 的功能,我们可以将其最初传递给 mk-length .
B:这是正确的想法.然后,在 eternity 上调用 mk-length ,在 cdr 上调用此结果,以便再获得一个塔.
A:然后这仍然是 length 0

A: What if we could create another application of mk-length to eternity at this point?
B: That would only postpone the problem to one, and besides, how could we do that?
A: Well, since nobody cares what function we pass to mk-length we could pass it mk-length initially.
B: That's the right idea. And then we invoke mk-length on eternity and the result of this on the cdr so that we get one more piece of the tower.
A: Then this is still length0

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

B:是的,我们甚至可以使用 mk-length 代替 length :

B: Yes, we could even use mk-length instead of length:

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

A:我们为什么要这样做?
B:所有名称都是相等的,但是有些名称比其他名称更相等.
A:是的:只要我们使用一致的名称,就可以了.
B:并且 mk-length 的名称比 length 更加平等.如果我们使用 mk-length 之类的名称,则不断提醒我们 mk-length 的第一个参数是 mk-length .

A: Why would we want to do that?
B: All names are equal, but some names more equal than others.
A: True: as long as we use the names consistently, we are just fine.
B: And mk-length is a far more equal name than length. If we use a name like mk-length, it is a constant reminder that the first argument to mk-length is mk-length.

页,他们获得了一个适用于任何长度列表的版本. 的工作方式在另一个问题中有更详细的描述:

On page 166, the authors show a version that works for lists of length 0 or 1 (i.e., ≤1). Finally, on page 167, they get to a version that works on lists of any length. How that works is described in more detail in another question:

您可能还会发现以下有趣的问题:

You might also find the following questions of interest:

  • how to do this length<=1 more than once? (about page 166's length≤1)
  • The mechanism of anonymous function to call itself of Scheme? (touches on the eternity function used on page 166's length≤1)

这篇关于小计划者:length0和mk-length的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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