Deferred Evaluation

In functional programming, it is expected that a function should always return the same value for a given set of arguments. Furthermore, there are circumstances in which, if the evaluation of a given operation is delayed until the last possible moment at which the value is needed, certain action can be eliminated oe done in a more optimal manner. The principle of deferred evaluation, also called lazy evaluation, is to avoid actually performing a computation until it's value is needed, and to never repeat the same computation. Many FP language use lazy evaluation by default; Scheme, on the other hand, does not, but does allow you to use it explicitly using two forms, (delay) and (force).

(delay) accepts a expression and returns a marker called a promise, which represents the expression. When (force) is applied to a promise, the first time, it causes the expression to be evaluated, and memoizes - that is to say, caches - the result. Subsequent applications of (force) to the same promise return the memoized value.

Contents Previous Next