views:

301

answers:

3

Hi to everyone!

I'm working in a homework, and the professor asked me to implement the evaluation strategy called "call by name" in scheme in a certain language that we developed and he gave us an example at http://www.scala-lang.org/node/138 in the scala language, but i don't understand in what consists the call by name evaluation strategy? what differences it has with call by need?

thanks, greetings

+8  A: 

Call-by-need is a memoized version of call-by-name (see wikipedia).

In call-by-name, the argument is evaluated every time it is used, whereas in call-by-need, it is evaluated the first time it is used, and the value recorded so that subsequently it need not be re-evaluated.

Doug Currie
i already have implemented call by need, and when i was doing it the first implementation was without caching, i don't make sense to me that the professor asks me to do something that i already have done, because that i want to understand the truly difference between call by need and call by name
forellana
i confirmed with the professor, that is call by name, i was confused because we already write that code and now he is asking us again for that
forellana
+3  A: 

Call by name is a a parameter passing scheme where the parameter is evaluated when it is used, not when the function is called. Here's an example in pseudo-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.
}

The argument expression is lazily evaluated when accessed using the current values of the argument expression.

Richard Pennington
Lazy evaluation is evaluate at most once, call-by-name is evaluate zero, one or more times.
Randall Schulz
not necessarily: http://en.wikipedia.org/wiki/Lazy_evaluation
Richard Pennington
I don't know about Scheme, but in Scala (which has no lazy parameters), that distinction is entirely accurate.
Randall Schulz
+1  A: 

Add this to the above answers:

Work through the SICP section on Streams. It gives a good explanation of both call-by-name and call-by-need. It also shows how to implement those in Scheme. BTW, if you are looking for a quick solution here is a basic call-by-need implemented in 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))

A sample run:

> (define lazy (delay (+ 3 4)))
> (force lazy) 
computing ... ;; Computes 3 + 4 and memoizes the result.
7
> (my-force lazy) 
7 ;; Returns the memoized value.
Vijay Mathew
`delay` and `force` are r5rs, though? actually, at least as old as r3rs.
Cirno de Bergerac
@sgm yes, they are part of the standard. I just wanted to show how they can be implemented.
Vijay Mathew