19

我正在做一项家庭作业,要求我们以我们开发的某种语言(使用 Scheme)实施一种称为“按名称调用”的评估策略。

我们在 Scala 中给出了一个示例,但我不明白“按名称调用”如何工作以及它与“按需要调用”有何不同?

4

3 回答 3

28

Call-by-need 是 call-by-name 的记忆版本(参见wikipedia)。

在按名称调用中,每次使用参数时都会对其进行评估,而在按需调用中,在第一次使用时对其进行评估,并记录该值,以便随后无需重新评估。

于 2010-06-03T02:44:08.723 回答
14

按名称调用是一种参数传递方案,其中参数在使用时进行评估,而不是在调用函数时进行评估。这是伪 C 中的一个示例:

int i;
char array[3] = { 0, 1, 2 };

i = 0;
f(a[i]);

int f(int j)
{
    int k = j;    // k = 0
    i = 2;        // modify global i
    k = j;        // The argument expression (a[i]) is re-evaluated, giving 2.
}

使用参数表达式的当前值访问参数表达式时,会延迟计算参数表达式。

于 2010-06-03T02:46:25.303 回答
3

将此添加到上述答案中:

完成有关 Streams的SICP 部分。它很好地解释了按名称调用和按需要调用。它还展示了如何在 Scheme 中实现这些。顺便说一句,如果您正在寻找快速解决方案,这里是在 Scheme 中实现的基本按需调用:

 ;; Returns a promise to execute a computation. (implements call-by-name)
 ;; Caches the result (memoization) of the computation on its first evaluation
 ;; and returns that value on subsequent calls. (implements call-by-need)
 (define-syntax delay
    (syntax-rules ()
      ((_ (expr ...))
       (let ((proc (lambda () (expr ...)))
             (already-evaluated #f)
             (result null))
         (lambda ()
           (if (not already-evaluated)
               (begin
                 (display "computing ...") (newline)
                 (set! result (proc))
                 (set! already-evaluated #t)))
           result)))))

 ;; Forces the evaluation of a delayed computation created by 'delay'.
 (define (my-force proc) (proc))

示例运行:

> (define lazy (delay (+ 3 4)))
> (force lazy) 
computing ... ;; Computes 3 + 4 and memoizes the result.
7
> (my-force lazy) 
7 ;; Returns the memoized value.
于 2010-06-03T05:09:20.467 回答