如何从 SICP 调用方案编号函数
在 SICP(例如 2.6)中,以下功能被描述为“无需数字即可度过难关”的方式。 我正在努力理解这一点。 首先,如何调用这些函数? 我实际上可以以某种方式应用它们,输出将为 1 吗? (或任何其他数字?)
(define zero (lambda (f) (lambda (x) x)))
(define (add-1 n)
(lambda (f) (lambda (x) (f ((n f) x)))))
我最初的尝试没有成功:
Welcome to DrScheme, version 4.1.5 [3m].
Language: Simply Scheme; memory limit: 128 megabytes.
> (add-1 (zero))
. . procedure zero: expects 1 argument, given 0
> (add-1 zero)
#<procedure>
> (add-1 1)
#<procedure>
> ((add-1 1))
. . #<procedure>: expects 1 argument, given 0
>
In SICP, (ex 2.6) the following functions are described as ways of 'getting by without numbers'. I'm scratching trying to understand this. As a starting point, how do these functions get invoked? Can I actually apply them in some way where the output will be 1? (Or any other number?)
(define zero (lambda (f) (lambda (x) x)))
(define (add-1 n)
(lambda (f) (lambda (x) (f ((n f) x)))))
My initial attempts haven't been successful:
Welcome to DrScheme, version 4.1.5 [3m].
Language: Simply Scheme; memory limit: 128 megabytes.
> (add-1 (zero))
. . procedure zero: expects 1 argument, given 0
> (add-1 zero)
#<procedure>
> (add-1 1)
#<procedure>
> ((add-1 1))
. . #<procedure>: expects 1 argument, given 0
>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这些表示数字的函数称为“Church 数字”(如 SICP 规定)。 它们的存在意味着您可以定义一个计算系统(例如 lambda 演算),而无需将数字作为第一类对象 - 您可以使用函数作为原始对象。 这一事实主要具有理论意义; 对于实际计算来说,教堂数字并不是一个好的选择。
通过将丘奇数字与其他对象作为参数应用,您可以看到丘奇数字定义的正确性。 当您将代表 n 的丘奇数字应用于函数 f 时,您会得到另一个将 f 应用于其参数 n 次的函数,例如,对于 n=3,为 f(f(f(x)))。
These functions that represent numbers are called Church numerals (as SICP states). Their existence means that you can define a system of computation (such as the lambda calculus) without having numbers as first-class objects--you can use functions as the primitive objects instead. This fact is mainly of theoretical interest; Church numerals are not a good choice for practical computation.
You can see the correctness of your definitions of Church numerals by applying them with other objects as arguments. When you apply a Church numeral representing n to a function f, you get another function that applies f to its argument n times, e.g., f(f(f(x))) for n=3.
这是原始的 lambda 演算,它不产生数字,它完全用函数替换数字类型。
所以,你有一个“零”函数,如果你对它调用 add-1 ,你不会得到 1,你会得到另一个代表 1 的函数。关键是生成的函数符合与基本算术公理,所以它们等价于自然数
this is the original lambda calculus it doesn't produce numbers, it totally replaces the number type with functions.
so, you have a 'zero' function, and if you call
add-1
to it, you won't get 1, you get another function that represents 1. the point is that the functions produced comply with the basic arithmetic axioms, so they're equivalent to natural numbers和输出:
and the output: