The lazy evaluation is a useful evaluation strategy adopted from different programming languages, it consists on do not evaluate an expression unless its value is needed to the rest of program.
This strategy is used by a lot of functional languages like Haskell, but today some functional behaviours are implemented in modern languages like python, ruby etc..
For example in Python 3.x the range() function returns a special range object which computes elements of the list on demand (lazy or deferred evaluation)
Let us see in action the laziness
We could define a âmy_fake_ifâ function like that
fun my_fake_if (x , y , z) = if x then y else z;
The âyâ term will be evaluated only if the âxâ term evaluation returns true, otherwise it will not evaluated at all.
So now we could write a program that uses my_fake_if
my_fake_if (true, 100 , 1 div 0);
and the result of this program âin a lazy worldâ will be obviously 100.
In depth
the third term ( aka âyâ ) contains a zero division so an eager evaluation strategy will throws an exception.
But not all functional languages are lazy, in fact this is an SML example (Standard ML is an eager functional language) and the execution of my_fake_if(true, 100 , 1 div 0) Â throws that
uncaught exception Div [divide by zero]
The lazy evaluation can lead a lot of benefits such as
Making potentials infinite data structures
Reducing the memory footprint and execution time provided by the non-evaluation of unused terms
Structure and interpretation of computer programs - Lazy evaluation
Haskell (Lazy)
SML (Eager)