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 Global Variables in Lisp

Defining Global Variables in Lisp

As the player calls the functions that make up our game, the program will need to track the small and big limits. In order to do this, we’ll need to create two global variables called *small* and *big*.

Defining the small and big Variables

A variable that is defined globally in Lisp is called a top-level definition. We can create new top-level definitions with the defparameter function:

> (defparameter *small* 1)
*SMALL*
> (defparameter *big* 100)
*BIG*

The function name defparameter is a bit confusing, since it doesn’t really have anything to do with parameters. What it does is let you define a global variable.

The first argument we send to defparameter is the name of the new variable. The asterisks surrounding the names *big* and *small*—affectionately called earmuffs—are completely arbitrary and optional. Lisp sees the asterisks as part of the variable names and ignores them. Lispers like to mark all their global variables in this way as a convention, to make them easy to distinguish from local variables, which are discussed later in this chapter.


  

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