如何从 SICP 调用方案编号函数

发布于 2024-07-18 11:03:25 字数 602 浏览 3 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼中杀气 2024-07-25 11:03:25

这些表示数字的函数称为“Church 数字”(如 SICP 规定)。 它们的存在意味着您可以定义一个计算系统(例如 lambda 演算),而无需将数字作为第一类对象 - 您可以使用函数作为原始对象。 这一事实主要具有理论意义; 对于实际计算来说,教堂数字并不是一个好的选择。

通过将丘奇数字与其他对象作为参数应用,您可以看到丘奇数字定义的正确性。 当您将代表 n 的丘奇数字应用于函数 f 时,您会得到另一个将 f 应用于其参数 n 次的函数,例如,对于 n=3,为 f(f(f(x)))。

> (define (double x) (* 2 x))
> (zero double)
#<procedure>
> ((zero double) 1)
1
> ((zero double) 100)
100
> (define one (add-1 zero))
> ((one double) 1)
2
> ((one double) 100)
200
> (define (cons-a x) (cons 'a x))
> ((zero cons-a) '())
()
> (((add-1 one) cons-a) '(1 2 3))
(a a 1 2 3)

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.

> (define (double x) (* 2 x))
> (zero double)
#<procedure>
> ((zero double) 1)
1
> ((zero double) 100)
100
> (define one (add-1 zero))
> ((one double) 1)
2
> ((one double) 100)
200
> (define (cons-a x) (cons 'a x))
> ((zero cons-a) '())
()
> (((add-1 one) cons-a) '(1 2 3))
(a a 1 2 3)
八巷 2024-07-25 11:03:25

这是原始的 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

旧时光的容颜 2024-07-25 11:03:25
(define (as-primitive-num church-num)
    (define (inc a) (+ a 1))
    ((church-num inc) 0))

;testing:
(define one (add-1 zero))
(define two (add-1 one))

(display (as-primitive-num one)) (newline)
(display (as-primitive-num two)) (newline)

和输出:

    1
    2
(define (as-primitive-num church-num)
    (define (inc a) (+ a 1))
    ((church-num inc) 0))

;testing:
(define one (add-1 zero))
(define two (add-1 one))

(display (as-primitive-num one)) (newline)
(display (as-primitive-num two)) (newline)

and the output:

    1
    2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文