Next: , Up: Welcome to Lisp   [Contents][Index]


16.4.1 Form

Any lisp system will include an interactive front-end called a toplevel. You type Lisp expressions into the toplevel, and the system displays their values.

Lisp usually displays a prompt to tell you that it’s waiting for you to type something. Many implementations of Common Lisp use ‘>’ as the toplevel prompt.

Lisp Expressions

One of the simplest kinds of Lisp expression is the integer. If you enter ‘1’ after the prompt,

> 1
1
>

the system will print its value, followed by another prompt.

In this case, the value displayed is the same as what we typed. An number is said to evaluate to itself. Things get more interesting when you enter expressions that take some work to evaluate. If you want to add two numbers together, you type something like:

> (+ 2 3)
5

The ‘+’ is called the operator, and the numbers ‘2’ and ‘3’ are called the arguments.

This form is called prefix notation, because the operator comes first. The flexibility of prefix notation means that, in Lisp, ‘+’ can take any number of arguments, including none:

> (+)
0
> (+ 2)
2
> (+ 2 3)
5
> (+ 2 3 4)
9
> (+ 2 3 4 5)
14

Because operators can take varying numbers of arguments, we need parentheses to show where an expression begins and ends.

Nested Expressions

Expressions can be nested. That is, the arguments in an expression may themselves be complex expressions:

> (/ (- 7 1) (- 4 2))
3

Expressions and Code Consist of Atoms and Lists

Another beauty of Lisp notation is: this is all there is. All Lisp expressions are either:

All Lisp code takes this form. A language like C has a more complicated syntax: arithmetic expressions use infix notation; function calls use a sort of prefix notation, with the arguments delimited by commas; expressions are delimited by commas; and blocks of code are delimited by curly brackets. In Lisp, we use a single notation to express all these ideas.


Next: Evaluation, Up: Welcome to Lisp   [Contents][Index]