Casting Spels in Lisp

Categories:

Recommended

Every computer language has code that is made up of syntax and semantics. The syntax of a programming language is the basic skeleton your program has to follow so that the compiler knows what’s what in your program, so it can tell what’s a function, a variable, etc. The semantics of a program is the more “random” stuff, like the different commands you have available and what variables you’re allowed to look at at any point in the program. The first thing that is special about Lisp is that it has the simplest syntax of any major programming language.

Basically, the Lisp syntax dictates that any text you give the Lisp compiler has to be arranged into lists, which can be nested into lists of lists or such as needed. The parenthesis mark the beginning and end of each list:

Additionally, the Lisp compiler uses two modes when it reads your code: A Code Mode and a Data Mode. When you’re in Data Mode, you can put anything you want into your lists. However, the compiler first starts off reading your code in Code Mode- In Code Mode, your lists need to be a special type of list called a form:

A form is a list where the first symbol in the list has to be a special word that the compiler can understand- Usually the name of a function. In this case, the compiler will send the other items of the list to the function as parameters. When it reads the text for these parameters, it will usually assume that they are also in Code Mode, unless you tell it to flip into data mode.

Defining the Data for our Game World

In order to learn some more about forms, let’s create a some forms that create the data for our game world. First of all, our game is going to have some objects in it that the player can pick up and use- Let’s define those objects:
(setf *objects* ‘(whiskey-bottle bucket frog chain))

Ok, now let’s dissect this line an see what it means: Since a Lisp compiler always starts reading things in Code Mode and expects a form, the first symbol, setf, must be a command. In this case, the command sets a variable to a value: The variable is *objects* (Lispers like to put stars around the names for global variables as a convention) and the value we are setting it to is a list of the four objects. Now, since the list is data (i.e. we don’t want the compiler to try and call a function called whiskey-bottle) we need to “flip” the compiler into Data Mode when reading the list.

Category:

Attribution

Conrad Barski, M.D. Casting Spels in Lisp. http://www.lisperati.com/casting.html

VP Flipbook Maker

Convert your work to digital flipbook with VP Online Flipbook Maker! You can also create a new one with the tool. Try it now!