gpt4 book ai didi

scheme - SICP 练习 1.5

转载 作者:行者123 更新时间:2023-12-04 10:09:51 27 4
gpt4 key购买 nike

Exercise 1.5. Ben Bitdiddle has invented a test to determine whether the interpreter he is faced with is using applicative-order evaluation or normal-order evaluation. He defines the following two procedures:

(define (p) (p))

(define (test x y) (if (= x 0) 0 y))

Then he evaluates the expression

(test 0 (p))

What behavior will Ben observe with an interpreter that uses applicative-order evaluation? What behavior will he observe with an interpreter that uses normal-order evaluation?



我理解练习的答案;我的问题在于如何解释 (p) 与 p。例如, (test 0 (p)) 导致解释器挂起(这是预期的),但是具有上述定义的 (test 0 p) 立即计算为 0。为什么?

此外,假设我们将定义更改为 (define (p) p)。根据给定的定义,(test 0 (p)) 和 (test 0 p) 都评估为 0。为什么会发生这种情况?为什么解释器没有挂起?我正在将 Racket 博士与 SICP 包一起使用。

最佳答案

p是一个函数。 (p)是对函数的调用。

在您的口译员中评估 p .

p <Return>
==> P : #function

现在评价 (p) .确保你知道如何杀死你的口译员! (可能在 Dr. Racket 中有一个“停止”按钮。)
(p)

请注意,没有任何 react 。或者,至少,没有什么可见的。解释器正在旋转,消除尾调用(因此,使用接近 0 的内存),调用 p .

p(p)评估不同的事情,你应该期待不同的行为。

至于你的第二个问题:你正在定义 p成为一个返回自身的函数。再次尝试评估 p(p)与您的 (define (p) p)看看你得到了什么。我的猜测(我使用的是一台无法安装任何东西并且没有任何方案的计算机)是它们将评估为相同的东西。 (我什至可以打赌 (eq? p (p)) 将评估为 #t 。)

关于scheme - SICP 练习 1.5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11334611/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com