You are previewing Land of Lisp.

Land of Lisp

Cover of Land of Lisp by Conrad Barski M.D. Published by No Starch Press

The Conditionals: if and Beyond

Now that you understand how Lisp handles true and false, let’s look at if and some of the other conditional commands.

One Thing at a Time with if

The if command can be used to make different things happen when things are true (such as when 1 + 2 = 3) or false (such as when 1 + 2 = 4).

> (if (= (+ 1 2) 3)


> (if (= (+ 1 2) 4)


The if command can also be used to check whether a list is empty:

> (if '(1)


> (if '()


So far, the only way to branch on a condition that we’ve looked at has been the if command:

> (if (oddp 5)


All ...

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