How Lisp Distinguishes Between Code and Data

When we write our Lisp program, how does Lisp decide which parts of our program consist of code (stuff to be executed) and which parts are just data? The syntax of Lisp has a special way of distinguishing between the two.

Common Lisp uses two modes when it reads your code: a code mode and a data mode. You can switch between these two modes when writing Lisp code.

Code Mode

Whenever you type something into the Lisp REPL, the compiler assumes that you’re entering a command you want to execute. In other words, Lisp always assumes that you’re writing code and defaults to code mode.

As we’ve already discussed, Lisp will expect Lisp code to be entered as a list. However, the code should be in a special type of list: a form. So when you’re in code mode, as you are when you start typing into the REPL, the commands you enter need to be structured as forms:

image with no caption

A form is simply a list with a special command at the beginning—typically the name of a function.

When reading a form, Lisp sends all other items in the list to the function as parameters. For example, enter the following into your REPL:

> (expt 2 3)
8

This calculates 2^3 = 8. It does this by calling expt, which computes an exponent. This command was entered in the standard way for Lisp: as a form with the function name at the beginning.

When Lisp reads the text for the parameters of such a command, it usually assumes that these parameters are also in code mode. Here’s an example:

> (expt 2 (+ 3 4))
128

This example has two nested forms. Lisp first looks at the entire expression in code mode. It determines that we’ve entered a form for the expt command. Then Lisp looks at the arguments to this command, also in code mode. One of these arguments (+ 3 4) is a form in its own right. This form is then executed, yielding 7. Afterward, this result is passed to the outer expt form, which is then executed.

Data Mode

As you might imagine, any stuff written in data mode is treated as data. This means the computer will not try to “execute” it, which allows us to have information in our code that’s just plain old data.

Let’s take a look at data mode in action. We’ll enter the same form that we entered in code mode in the previous example, with one difference:

> '(expt 2 3)
(expt 2 3)

This time, we put a single quote in front of the list. Instead of responding with the sum of the numbers 1 and 2, Lisp simply parrots our expression to us. The single quote tells Lisp to treat the subsequent form as a chunk of data—simply a list of items. Lisp then prints the result of evaluating what we entered, which is the list itself. It ignores any functions or variables in our list, treating everything as data.

Placing a quote in front of lists so that they won’t be evaluated as a command is called quoting. By using quoting, you can tell Lisp, “This next part isn’t a command. It’s just a chunk of data for my program.”

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.137.67