Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!uwm.edu!bionet!ig!arizona!gudeman From: gudeman@cs.arizona.edu (David Gudeman) Newsgroups: comp.lang.misc Subject: Re: What is Lazy Evaluation? Message-ID: <17909@megaron.cs.arizona.edu> Date: 14 Feb 90 09:31:31 GMT Organization: U of Arizona CS Dept, Tucson Lines: 51 In article <3109@caesar.cs.montana.edu> icsu8209@caesar.cs.montana.edu (Lou Glassy) writes: >What does 'lazy evaluation' mean? Is it the same thing as 'short-circuit evaluation? No, but they are related. Short-circuit evaluation, as you said, involves avoiding some computations. The most common example is boolean operations such as you showed, however there are other examples such as x := y * f(x); where f(x) does not have to be called if y = 0 (ignoring the issue of side-effects). Lazy evaluation is usually associated with parameter passing. For example, in the function function f(x,y) = if x > 1 then x else y + y given the call f(h(z),g(z)) it is not necessary to ever call g(z) if h(z) > 1. This is like call-by-name in some respects, in that call-by-name also does not evaluate arguments that are never needed, but with lazy evaluation (call-by-need) the expression g(z) is evaluated at most once. The evaluation sequence goes something like this: 1 call f(h(z),g(z)) where the arguments are passed unevaluated 2 try to evaluate (x > 1) 3 the value of x is needed, so call h(z) and assign the result to x 4 evaluate (x > 1) to get the result 5 (assume the result is TRUE) return the value stored at x In call-by-name, the steps are 1 call f(h(z),g(z)) where the arguments are passed unevaluated 2 evaluate (x > 1) as (h(x) > 1) 3 (assume the result is TRUE) return the value of x as h(x) In the call-by-name version, h(x) is called twice. There are other forms of lazy evaluation as well, the term generally means, "when you encounter an expression, don't evaluate the expression until you actually need for it the first time, then store the result in case you need it again." -- David Gudeman Department of Computer Science The University of Arizona gudeman@cs.arizona.edu Tucson, AZ 85721 noao!arizona!gudeman