Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

4. Making Decisions with Conditions > The Conditionals: if and Beyond

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)
      'yup
      'nope)

YUP

> (if (= (+ 1 2) 4)
      'yup
      'nope)

NOPE

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

> (if '(1)
      'the-list-has-stuff-in-it
      'the-list-is-empty)

THE-LIST-HAS-STUFF-IN-IT

> (if '()
      'the-list-has-stuff-in-it
      'the-list-is-empty)

THE-LIST-IS-EMPTY

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


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint