[Post updated to reflect comments. …too much late night typing…]

Lazy evaluation is a very novel aspect of Haskell. Turns out that it’s not that difficult to think about.

A very common example of lazy-ish evaluation is ‘&&’ operators used in lots of languages (using C#):



if ( (obj != null) && (obj.someMethod() == somethingElse) ) { // do something }

_obj.someMethod_ isn’t evaluated unless _obj != null_. So _&&_, a C# function/operator everyone knows and loves, is strict in its first parameter and lazy in its second. The previous code might act a little like the following:



if (obj != null){ if (obj.someMethod() == somethingElse) ) { // do something } }

If _&&_ were strict in both parameters, then it’d throw errors when the second parameter was unconditionally evaluated (e.g. _null.someMethod()). If _&&_ weren’t lazy, it might act something like:



condition1 = (obj != null); condition2 = (obj.someMethod() == somethingElse); if (condition1 && condition2) { // do something } }

Clearly, _condition2_ would throw an Exception when _obj_ was null, so the _if_ statement must not be evaluating the second parameter. It’s as if it were lazy…

Here’s the (slightly revised for clarity) definition of _&&_ from Haskell:



(&&) :: Bool -> Bool -> Bool (&&) True x = x (&&) False _ = False

Just as with C#, the first parameter (_x_) must be evaluated, but the second parameter is only evaluated if the first parameter is True. _&&_ is *strict* in the first parameter and *lazy* in the second.

### And That’s All There Is to Laziness?

Nope. It’s “turtles all the way down”.

Laziness is a defining feature of Haskell and a feature that separates Haskell from the vast majority of languages. Lazy evaluation isn’t confined to a few operators in Haskell; lazy evaluation starts at the first function/expression in your program and continues all the way down.

For more reading, check out here, here, here and here.

### But I Don’t Want To Be Lazy

[Example fixed.]

Then force evaluation of your parameters. How? One way is to use the _$!_ function to force the evaluation of parameters.



f a b c = ((someFunction $! a) $! b) $! c

See here for more.

—

\* These little bits have helped me think about Haskell. Maybe they’ll be useful for you, too.