Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!tut.cis.ohio-state.edu!pt.cs.cmu.edu!sei!firth From: firth@sei.cmu.edu (Robert Firth) Newsgroups: comp.lang.misc Subject: Re: What is Lazy Evaluation? Keywords: expression evaluation, lazy evaluation Message-ID: <6081@bd.sei.cmu.edu> Date: 13 Feb 90 19:09:13 GMT References: <3109@caesar.cs.montana.edu> Reply-To: firth@sei.cmu.edu (Robert Firth) Distribution: usa Organization: Software Engineering Institute, Pittsburgh, PA Lines: 44 In article <3109@caesar.cs.montana.edu> icsu8209@caesar.cs.montana.edu (Glassy) writes: >What does 'lazy evaluation' mean? Is it the same thing as 'short-circuit evaluation? I'll have a shot at an answer. (a) 'short-circuit' versus 'full' evaluation is usually considered a compile time issue, ie the compiler generates one of two code sequences. Example: if x>=0 and then sqrt(x)<=y ... this is short circuit evaluation, something like test x branch negative L call sqrt(x) compare y branch greater-than L ... L: so if the first test is enough to decide the condition, the second is not performed. By contrast, full evaluation always evaluates everything. (b) 'lazy' versus 'greedy' evaluation is usually considered an issue in implementing language interpreters, eg for lambda calculus. Greedy evaluation evaluates an expression as soon as it can; lazy evaluation as late as it can. Example: munge(1/x) greedy evaluation will evaluate 1/x and then call munge(). lazy evaluation will call munge(), and evaluate 1/x only when the body of munge referenced its formal parameter. As the example shows, lazy evaluation "works" in cases where greedy evaluation does not - in this case, if x=0 but munge doesn't reference its parameter. Hope that helps.