应用顺序/按值调用和正常顺序/按名称调用的区别 [英] applicative-order/call-by-value and normal-order/call-by-name differences

查看:140
本文介绍了应用顺序/按值调用和正常顺序/按名称调用的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在根据在线课程学习sicp,但对其讲义感到困惑.在讲义中,应用顺序似乎等于cbv和正常顺序等于cbn.

I am learning the sicp according to an online course and got confused by its lecture notes. In the lecture notes, the applicative order seems to equal cbv and normal order to cbn.

但是 wiki 指出,除了评估顺序(从左到右,从右到右)向左或同时),则应用顺序和cbv之间是有区别的:

But the wiki points out that, beside evaluation orders(left to right, right to left, or simultaneous), there is a difference between the applicative order and cbv:

与按值调用不同,应用顺序评估在应用函数之前尽可能地减少了函数体内的术语.

Unlike call-by-value, applicative order evaluation reduces terms within a function body as much as possible before the function is applied.

我不明白减少意味着什么.在进行函数求值之前,应用顺序和cbv都不会获取变量的确切值.

对于正常订单和cbv,根据 wiki ,我更加困惑

And for the normal order and cbv, I am even more confused according to wiki.

相比之下,按名称调用策略不会在未应用函数的主体内部求值.

In contrast, a call-by-name strategy does not evaluate inside the body of an unapplied function.

我想这是否意味着正常顺序将在未应用函数的主体内部求值.怎么可能?

  1. 有人可以给我一些关于这四种策略的更具体的定义.
  2. 有人可以使用任何编程语言来展示每种策略的示例.

非常感谢吗?

推荐答案

应用顺序(不考虑 评估顺序(在方案中未定义)将等同于cbv.进入函数主体之前,将对函数调用的所有参数进行全面评估.这是给出的例子 SICP

Applicative order (without taking into account the order of evaluation ,which in scheme is undefined) would be equivalent to cbv. All arguments of a function call are fully evaluated before entering the functions body. This is the example given in SICP

(define (try a b)
  (if (= a 0) 1 b))

如果定义函数,并使用以下参数调用它:

If you define the function, and call it with these arguments:

(try 0 (/ 1 0))

使用适用性订单评估(方案中的默认设置)时,这会产生错误.它将评估 (/ 1 0)进入身体之前.在使用正常订单评估时,它将返回1. 将不经评估传递给函数主体,并且由于(= a 1)为true,因此不会评估(/ 1 0),从而避免了错误.

When using applicative order evaluation (default in scheme) this will produce and error. It will evaluate (/ 1 0) before entering the body. While with normal order evaluation, this would return 1. The arguments will be passed without evaluation to the functions body and (/ 1 0) will never be evaluated because (= a 1) is true, avoiding the error.

在您链接到的文章中,当他们提到应用顺序"和正常"顺序评估时,他们谈论的是Lambda微积分.在本文中 wiki 我认为它可以更清楚地解释. 精简意味着将精简规则应用于表达式. (也在链接中).

In the article you link to, they are talking about Lambda Calculus when they mention Applicative and Normal order evaluation. In this article wiki It is explained more clearly I think. Reduced means applying reduction rules to the expression. (also in the link).

α转换:更改绑定变量(alpha);

α-conversion: changing bound variables (alpha);

β约简:将函数应用于其参数(β);

β-reduction: applying functions to their arguments (beta);

正常订单:

总是最先减少最左边,最外面的redex.也就是说,在减少参数之前,只要有可能将参数替换为抽象的主体.

The leftmost, outermost redex is always reduced first. That is, whenever possible the arguments are substituted into the body of an abstraction before the arguments are reduced.

按姓名致电

按照正常顺序,但是抽象内部不执行任何归约.例如,虽然λx.(λx.x)x包含redex(λx.x)x,但根据此策略仍是标准形式.

As normal order, but no reductions are performed inside abstractions. For example λx.(λx.x)x is in normal form according to this strategy, although it contains the redex (λx.x)x.

正常形式是一个等效表达式,根据该形式施加的规则不能进一步简化

A normal form is an equivalent expression that cannot be reduced any further under the rules imposed by the form

在同一篇文章中,他们谈到了按价值致电

In the same article, they say about call-by-value

仅减少最外面的redex:仅当其右手边减小到某个值(变量或lambda抽象)时,redex才减少.

Only the outermost redexes are reduced: a redex is reduced only when its right hand side has reduced to a value (variable or lambda abstraction).

和适用顺序:

最左边,最里面的redex总是最先减少.从直觉上讲,这意味着函数的参数总是在函数本身之前被减少.

The leftmost, innermost redex is always reduced first. Intuitively this means a function's arguments are always reduced before the function itself.

您可以阅读我链接的文章,以获取有关lambda微积分的更多信息. 另外编程语言基础

You can read the article I linked for more information about lambda-calculus. Also Programming Language Foundations

这篇关于应用顺序/按值调用和正常顺序/按名称调用的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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