寻求SICP练习1.5的一些解释 [英] seek for some explanation on SICP exercise 1.5

查看:62
本文介绍了寻求SICP练习1.5的一些解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以找到该问题这里.

在书中,我发现对正常订单评估的一种描述是:

另一种评估模型不会在需要操作数之前对其进行评估.相反,它将首先用操作数表达式替换参数,直到获得仅包含基本运算符的表达式,然后执行评估."

我还简短地找到了另一个描述:完全展开然后减小".

在练习中,我认为p的定义类似于(lambda () (p)), 永远不会扩展为原始运算符,因此永远不会终止.

但是,另一方面,在搜索了该问题的一些答案之后,正常订单评估似乎应该终止,因为它仅根据需要评估事物,而实际上不会评估(p).

因此,我认为扩展"和评估"之间必须有一些区别,解释器在这里所​​做的是评估事物.

到底有什么区别,或者 有一些我想念的地方吗?

另一个问题:我应该说"(p)被评估为(p)"还是"(p)被扩展为(p)"?

解决方案

您是对的,如果要求评估(p),则应用顺序评估器不会终止.但是,当前的问题提到if具有特定的评估语义,该语义由应用级和正常级评估者共享.具体地说,假设不管解释器使用的是正常顺序还是应用顺序,特殊格式的if的评估规则都是相同的:首先评估谓词表达式,然后结果决定是评估结果表达式还是替代表达式."

练习中的代码是

(define (p) (p))

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

正在考虑的测试是

(test 0 (p))

正常顺序评估是完全展开然后减小"选项.在正常顺序评估下,(test 0 (p))完全扩展为

(test 0 (p)) ==
(if (= 0 0)
  0
  (p))

由于if具有上述语义,并且扩展中的测试条件为(= 0 0),这是正确的,因此正常级评估器确定对结果进行评估,即0,因此,表达式是0.

但是,使用应用顺序评估,评估(test 0 (p))的第一步是评估表达式test0(p),然后调用("apply" ,因此"applicative" )test值与通过评估0(p)产生的值相同.由于(p)的评估不会完成,因此(test 0 (p))的评估也不会完成.

The question can be found here.

In the book, I found one description of normal order evaluation was:

"An alternative evaluation model would not evaluate the operands until their values were needed. Instead it would first substitute operand expressions for parameters until it obtained an expression involving only primitive operators, and would then perform the evaluation."

I also found another description in short: "fully expand and then reduce".

In the exercise, I thought the definition of p was something like (lambda () (p)), which would never expand to a primitive operator, thus never terminate.

However, on the other hand, after googling some answers to this question, it seems normal order evaluation should terminate because it only evaluates things as needed, and actually (p) won't be evaluated.

So I think there must be some difference between "expand" and "evaluate" and what the interpreter does here is to evaluate things.

What exactly is the difference, or are there some points that I have missed?

Another question: Should I say "(p) is evaluated to (p)" or "(p) is expanded to (p)"?

解决方案

You're right that an applicative-order evaluator would not terminate if asked to evaluate (p). However, the question at hand mentions that if has a particular evaluation semantics which is shared by the applicative and normal-order evaluators. Specifically, "Assume that the evaluation rule for the special form if is the same whether the interpreter is using normal or applicative order: The predicate expression is evaluated first, and the result determines whether to evaluate the consequent or the alternative expression."

The code from the exercise is

(define (p) (p))

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

and the test under consideration is

(test 0 (p))

Normal-order evaluation is the "fully expand and then reduce" option. Under normal-order evaluation, (test 0 (p)) is fully expanded as

(test 0 (p)) ==
(if (= 0 0)
  0
  (p))

Since if has the semantics described above, and the test condition in the expansion is (= 0 0), which is true, the normal-order evaluator determines to evaluate the consequent, which is 0, so the value of the expression is 0.

Using the applicative-order evaluation however, the first step in evaluating (test 0 (p)) is to evaluate the expressions test, 0, and (p), and then call ("apply", hence "applicative") the value of test with the values produced by evaluating 0 and (p). Since the evaluation of (p) will not complete, neither will the evaluation of (test 0 (p)).

这篇关于寻求SICP练习1.5的一些解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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