Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!sharkey!math.lsa.umich.edu!zaphod.mps.ohio-state.edu!uwm.edu!uwvax!daffy!cat19.cs.wisc.edu!schaut From: schaut@cat19.cs.wisc.edu (Rick Schaut) Newsgroups: comp.lang.misc Subject: Re: What is Lazy Evaluation? Keywords: expression evaluation, lazy evaluation Message-ID: <4317@daffy.cs.wisc.edu> Date: 18 Feb 90 23:34:18 GMT References: <3109@caesar.cs.montana.edu> Sender: news@daffy.cs.wisc.edu Distribution: usa Organization: U of Wisconsin CS Dept Lines: 59 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? Lazy evaluation is not the same as short-circuit evaluation. Lazy evaluation is where expressions are not evaluated until they are needed. For example, if you had the following function definition: Func Foo(x,y: integer): integer; var z integer; begin z := y + 5; return (z + x / 5); end; and call: a = Foo((3+4),7); the expression, (3 + 4) doesn't get evaluated until the formal parameter, x, is referenced in the return expression. This may seem inocuous, but consider the following: program var a: integer; Func Foo(x,y: integer): integer; begin a = 2; return( x * y + a); end; begin a = 3; Write(Foo((a + 4),7)); end. With normal evaluation, the output is 7 * 7 + 2, or 51. With lazy evaluation, however, the expression (a + 4) isn't evaluated until the return is executed in Foo. The output is 6 * 7 + 2, or 44. If you're interested in why someone might want lazy evaluation, see: Vuillemin, J. [1974]. Correct and optimal implimentation of recursion in a simple programming language. _J. Computer and System Sciences 9:3, 332-354 and Wadsworth, C [1971]. Semantics and Pragmatics of the Lambda Calculus. Ph. D. Thesis, Oxford Univ. [References from Sethi, R., _Programming Languages: Concepts and Constructs_, Addison-Wesley, 1989] -- Rick (schaut@garfield.cs.wisc.edu) Peace and Prejudice Don't Mix! (unknown add copy)