The Lazy Guild Frigate

Lisp Dialect

Clojure (available in Common Lisp with the Series library, CLAZY library, or custom macros)

image with no caption

Synopsis

A lazy programming language will perform a calculation only if the compiler determines it is absolutely necessary to produce a visible result. Clojure is the most popular Lisp dialect to include lazy programming as a primary feature. However, limited forms of lazy programming are common in all Lisp dialects.

How It Kills Bugs

Lazy languages let you create infinitely big data structures (as long as you don’t try to use all of the data), which allows more of your code to be formulated as transformations of large data structures. In general, it is easier to debug data structures than it is to debug algorithms. Algorithms involve steps that unfold over time, and to understand them, you usually need to watch them as they execute. Data, on the other hand, exists independently of time, which means you can find bugs in a data structure just by looking at it.

Example A-11. Example

(take 20 (filter even? (iterate inc 0)))

Note

This example is in the Clojure Lisp dialect and won’t run in Common Lisp.

Explanation

This code returns the first 20 even positive integers. To do this, it first creates an infinite list of all positive integers , using the iterate function to create a list of integers starting at zero. Then it filters out the even numbers . Finally, it takes the first 20 numbers from that result . Until the final take command, the data structures being operated on are theoretically infinite. However, since Clojure is a lazy language, it instantiates these data structures only on an as-needed basis. This means that only the first 20 such numbers are ever generated. (And even then, they are generated only if we actually use the final value somehow, such as printing it to the screen.)

Weakness

Since a lazy programming language chooses the order in which your code is run, it can lead to debugging headaches if you try to trace your code as it is running.

Chapter 18 discusses lazy programming.

image with no caption
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.49.247