A pure function produces the same result for the same arguments and has no observable side effects. This predictable contract makes function results safe to reuse, reason about, and memoize. section 2.4.2 section 20.5
Purity matters because it changes how functions can be used:
- Memoization can return a previously stored result when the same arguments recur, provided no outside context changes the answer. section 2.4.2
- A pure function passed to repeatedly would generate the same item repeatedly; use repeat when that is the intended behavior. section 9.2.1
- Functions treated as values can be wrapped transparently, making purity useful alongside higher-order functions. section 2.4.2
Memoizing a predictable function
A Levenshtein-distance function is wrapped with memoize. The first call for a pair computes and stores the result; a later call with the same pair reuses it. The example reports 400 cache hits after repeating the same search, while returning the same best matches.
Listing 2.25. → Caching, Function Wrapper, Function Generation, Performance
(memoize [f])
Inspect the compact memoize signature as a direct entry point for applying purity to cached function results. section 2.4.2


















