Chapter 10. Looping with the loop Command

The loop and format commands are powerful and hacker-friendly. Though most of the functionality they offer is available elsewhere in the Lisp language, these highly specialized commands are worth learning if you like terse code. We’ll look at loop in this chapter. The next chapter covers format.

The loop Macro

Any type of looping you would ever want to do inside a computer program can be accomplished with the loop macro. Here’s a simple example:

> (loop for i
        below 5
        sum i)
10

This code adds together the natural numbers below 5, like this:

0 + 1 + 2 + 3 + 4 = 10

You can see that this loop command doesn’t work in the way a proper Lisp command should. First of all, it’s parenthetically challenged. Never before ...

Get Land of Lisp now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.