Lazy Evaluation - Controlling Eagerness in Lazy Languages

Controlling Eagerness in Lazy Languages

In lazy programming languages such as Haskell, although the default is to evaluate expressions only when they are demanded, it is possible in some cases to make code more eager—or conversely, to make it more lazy again after it has been made more eager. This can be done by explicitly coding something which forces evaluation (which may make the code more eager) or avoiding such code (which may make the code more lazy). Strict evaluation usually implies eagerness, but they are technically different concepts.

However, there is an optimisation implemented in some compilers called strictness analysis, which, in some cases, allows the compiler to infer that a value will always be used. In such cases, this may render the programmer's choice of whether to force that particular value or not, irrelevant, because strictness analysis will force strict evaluation.

In Haskell, marking constructor fields strict means that their values will always be demanded immediately. The seq function can also be used to demand a value immediately and then pass it on, which is useful if a constructor field should generally be lazy. However, neither of these techniques implements recursive strictness—for that, a function called deepSeq was invented.

Also, pattern matching in Haskell 98 is strict by default, so the ~ qualifier has to be used to make it lazy.

Read more about this topic:  Lazy Evaluation

Famous quotes containing the words controlling, eagerness, lazy and/or languages:

    In controlling men:
    If at first you don’t succeed,
    Why, cry, cry, again.
    Unknown. A Maxim Revised (l. 3–4)

    PLAYING SHOULD BE FUN! In our great eagerness to teach our children we studiously look for “educational” toys, games with built-in lessons, books with a “message.” Often these “tools” are less interesting and stimulating than the child’s natural curiosity and playfulness. Play is by its very nature educational. And it should be pleasurable. When the fun goes out of play, most often so does the learning.
    Joanne E. Oppenheim (20th century)

    So, when my days of impotence approach,
    And I’m by pox and wine’s unlucky chance
    Forced from the pleasing billows of debauch
    On the dull shore of lazy temperance,
    My pains at least some respite shall afford
    While I behold the battles you maintain
    When fleets of glasses sail about the board,
    From whose broadsides volleys of wit shall rain.
    John Wilmot, 2d Earl Of Rochester (1647–1680)

    No doubt, to a man of sense, travel offers advantages. As many languages as he has, as many friends, as many arts and trades, so many times is he a man. A foreign country is a point of comparison, wherefrom to judge his own.
    Ralph Waldo Emerson (1803–1882)