You are previewing Land of Lisp.

Land of Lisp

Cover of Land of Lisp by Conrad Barski M.D. Published by No Starch Press
O'Reilly logo

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)

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 ...

The best content for your career. Discover unlimited learning on demand for around $1/day.