Chapter 6. Strict and Nonstrict Evaluations

Evaluations are the execution of a statement, usually the execution and setting of a variable. So what exactly does it mean to have a strict versus a nonstrict evaluation? Generally speaking, we as developers use strict evaluations. This means that the statement is immediately evaluated and assigned to the variable as soon as the variable is defined.

This obviously means that with nonstrict evaluations we don’t assign the variable where it is defined. This is also known as a “lazy” variable; the variable isn’t actually assigned until the first time it is used. This is really useful when we have variables that may not be used in a specific situation. Let’s look at a mathematical example.

Math Warning

Let’s assume that we have three functions: `a(x)`, `b(x)`, and `f(x)`.

If we look at this equation, we know to evaluate `b(x)` first because if it equals 0, there is no point in evaluating `a(x)` given that the entire equation fails. Our lazy value is `a(x)`, and this is the point of a lazy variable.

When thinking of lazy variables, we tend to also think of mutable variables, because we think of the variable being defined and eventually being set. We normally think about the Java example in Example 6-1. However, with nonstrict evaluation, we maintain immutability; the variable is assigned or evaluated only on the first reference. This means that before the variable ...

Get Becoming Functional now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.