I didn’t understand the diffrence between Call-by-name and Call-by-need. As I understood, Call-by-need method restores the answer returned. But how It helps us, and Is there any fundamental difference between the results ?
For example,
begin integer n;
procedure foo(e, n);
integer e, n;
begin
for n := 1 step 1 until 10 do begin
prints(`;;; the value of e is ');
printnln(e)
end
end;
foo(2 * n, n)
end
So in call-by-name, as I understood, We will get:
;;; the value of e is 2
;;; the value of e is 4
;;; the value of e is 8
and so on. This is because we pass 2*n to e, and e is evaluate with the new i everytime.
What would happen in call-by-need?
In call by need, we enter the loop, and evaluate the value only once. So In the code above, we will copy
(2*n)inside the loop (macro-style) and we will evaluate the expression only one time (Not like call by name).So, in the first iteration we will get
e=2. this will be the value ofealso in the next iteration, and the output will be: