gpt4 book ai didi

scheme - applicative-order/call-by-value 和 normal-order/call-by-name 的区别

转载 作者:行者123 更新时间:2023-12-04 02:04:58 24 4
gpt4 key购买 nike

背景

我正在根据在线类(class)学习 sicp,但被它的讲义弄糊涂了。在讲义中,applicative order似乎等于cbv,normal order等于cbn。

困惑

但是wiki指出,除了评估顺序(从左到右、从右到左或同时),应用顺序和 cbv 之间存在差异:

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

我不明白减少是什么意思。 applicative order 和 cbv 都不会在进入函数评估之前获取变量的确切值。

而对于正常的order和cbv,我根据wiki就更加糊涂了.

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

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

问题

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

非常感谢?

最佳答案

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

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

如果您定义函数,并使用这些参数调用它:

(try 0 (/ 1 0))

当使用应用顺序评估(方案中的默认值)时,这将产生错误。它会评估(/1 0) 进入正文之前。对于正常的顺序评估,这将返回 1。参数将在不评估的情况下传递给函数主体,并且 (/1 0) 永远不会被评估,因为 (= a 1) 为真,避免了错误。

在您链接到的文章中,当他们提到 Applicative 和 Normal order evaluation 时,他们在谈论 Lambda 微积分。本文中wiki我认为它解释得更清楚。减少意味着将减少规则应用于表达式。 (也在链接中)。

α-conversion: changing bound variables (alpha);

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

正常顺序:

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.

点名

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

在同一篇文章中,他们谈到了按值调用

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).

和应用顺序:

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

您可以阅读我链接的文章,了解有关 lambda 演算的更多信息。还有 Programming Language Foundations

关于scheme - applicative-order/call-by-value 和 normal-order/call-by-name 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26522960/

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