Any difference between Lazy evaluation and Short-circuit evaluation?
Asked Answered
M

1

22

From Wikipedia:

Lazy evaluation is:

In programming language theory, lazy evaluation or call-by-need is an evaluation strategy which delays the evaluation of an expression until its value is needed

Short-circuit evaluation is:

Short-circuit evaluation, minimal evaluation, or McCarthy evaluation denotes the semantics of some Boolean operators in some programming languages in which the second argument is only executed or evaluated if the first argument does not suffice to determine the value of the expression

So what's the difference between them for example when I have:

if(false && true && true) {
    //boo
} 

As far as I know, compiler doesn't execute expressions after false because I have && so the whole expression will be false finally. (right?)

So is that behavior called Lazy evaluation or Short-circuit evaluation?

Mathison answered 16/2, 2013 at 8:45 Comment(2)
You have some misunderstanding of what parsing actually refers to.Parallel
@Parallel Edited, I think executing is the correct word, isn't it?Mathison
C
23

The difference is that in case of lazy evaluation an expression is evaluated only when it is needed, while in case of short-circuit evaluation expression evaluation stops right after you know the result. It's sort of orthogonal notions.

Lazy evaluation can be applied to any computation (short-circuit scheme usually is used only with bools). It doesn't cut-off useless computation, but delays the whole computation until its result is required.

variable = bigAndSlowFunc() or evenSlowerFnc()
if (carry out heavy computations)
  print "Here it is: ", variable
else
  print "As you wish :-)"

If evaluation is lazy, variable will be computed only if we choose to go into the first (then) branch of if, otherwise it won't. At the evaluation stage (when we prepare arguments for print) short-circuit scheme can be used to decide if we need to call evenSlowerFnc.

So in your example, it's short-circuit evaluation since no delay of computation happen.

Cardiology answered 16/2, 2013 at 9:27 Comment(4)
Actually, the use of lazy evaluation can sometimes lead to cut-off of useless computation. For instance, when working with Django querysets, you can use filter (or exclude) to adjust the filtering criteria as many times as you need: the actual SQL query is only run when you access the result.Arundel
As the first sentence says, in lazy evaluation, an expression is evaluated only when its value needed. It follows that if the value of an expression is never needed, the expression is never evaluated. Consider f() && g(); when the value of this expression is needed, if f() evaluates to true, the computation of the value of g() is delayed until it is needed. Furthermore g() is not computed at all unless it is needed. Short-circuit evaluation is a simple case of lazy evaluation.Teteak
@TheodoreNorvell, in case of short-circuit evaluation g() is either evaluated, or not. It's not delayed at all. Well, okay, it's not executed until f() finishes, but this left-to-right execution is what one would expect when writing f() && g().Cardiology
You've restated my point, the evaluation of g() is delayed until and if it is needed. That's lazy evaluation (for the right operand). This happens to be right after f() is evaluated to true, but so what? Contrast it to E & F, where the evaluation model (in C) is evaluate E and F and then (bitwise) "and". Java is a bit different, imposing left to right evaluation of operands, but so what? Consider the definition of && in the Haskell prelude {True && x = x ; False && _ = False}. This behaves exactly as C's && because Haskell uses lazy evaluation.Teteak

© 2022 - 2024 — McMap. All rights reserved.