Lazy Evaluation - Laziness in Eager Languages

Laziness in Eager Languages

Python

In Python 2.x the range function computes a list of integers (eager or immediate evaluation):

>>> r = range(10) >>> print r >>> print r 3

In Python 3.x the range function returns an iterator which computes elements of the list on demand (lazy or deferred evaluation):

>>> r = range(10) >>> print(r) range(0, 10) >>> print(r) 3
This change to lazy evaluation saves execution time for large ranges which may never be fully referenced and memory usage for large ranges where only one or a few elements are needed at any time.

Python manifests lazy evaluation by implementing iterators (lazy sequences) unlike tuple or list sequences. For instance:

>>> list = range(10) >>> iterator = iter(list) >>> print list >>> print iterator >>> print iterator.next 0
The above example shows that lists are evaluated when called, but in case of iterator, the first element '0' is printed when need arises.
This section requires expansion.

Read more about this topic:  Lazy Evaluation

Famous quotes containing the words laziness, eager and/or languages:

    From passions grow opinions; intellectual laziness lets these harden into convictions.
    Friedrich Nietzsche (1844–1900)

    The three-year-old who lies about taking a cookie isn’t really a “liar” after all. He simply can’t control his impulses. He then convinces himself of a new truth and, eager for your approval, reports the version that he knows will make you happy.
    Cathy Rindner Tempelsman (20th century)

    It is time for dead languages to be quiet.
    Natalie Clifford Barney (1876–1972)