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

2. Creating Your First Lisp Program > Defining Local Variables in Lisp

Defining Local Variables in Lisp

For our simple game, we’ve defined global variables and functions. However, in most cases you’ll want to limit your definitions to a single function or a block of code. These are called local variables and functions.

To define a local variable, use the command let. A let command has the following structure:

 (let (variable declarations)
   ...body...)

The first thing inside the let command is a list of variable declarations . This is where we can declare one or more local variables. Then, in the body of the command (and only within this body), we can use these variables . Here is an example of the let command:

 > (let ((a 5)
         (b 6))
     (+ a b))
  11

In this example, we’ve declared the values 5 and 6 for the variables a and b , respectively. These are our variable declarations. Then, in the body of the let command, we added them together , resulting in the displayed value of 11.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

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

  

Start a Free Trial


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