|
|
|
|
home / intro / faq / tutorial / manual / models / download / people / about |
||
| the lisp interpreter |
|
Little b is defined as an extension of Lisp. This section provides an introduction to the Lisp interpreter and command prompt. (You can skip to the next section if this is familiar territory.) One of the most important features is the interpreter, a loop which reads a bit of text, computes its value and prints the result. This process is repeated indefinitely; it is called the READ-EVAL-PRINT loop (REPL). There's also a compiler which produces machine code, but we won't discuss it.
the command prompt The command prompt allows you to interact with the REPL. You have already seen a similar interface if you've used a calculator, an operating system command prompt, or an environment like MatLab or Mathematica. You type some text at the prompt, hit a key (usually enter), and the computer goes off and does something, possibly printing out some value to let you know the result or complaining about an error. It then returns, with another similar-looking prompt.
Let's add 3 and 4. (Lispers will notice that {3 + 4} is not Lisp code - this is an example of little b's infix syntax.) I've written code typed in by the user in a red fixed-width font. I've used this font to indicate key presses (e.g., [ENTER]), and this yellow sticky pad style to indicate comments.
|
B-USER 1> {3 + 4} [ENTER] 7 B-USER 2> | ← awaiting further input |
Great, the environment printed out the answer on the next line. The interpreter shows us the B-USER prompt again, and awaits further input.
This seems simple enough - how does this work? First, a bit of text - "{5 + 2}" - is read, producing a symbolic representation of the text called a form1, which is evaluated (this is where the addition is performed), producing a value which is printed. Not very complicated. This is the core operation of the Lisp interpreter (and hence b). A bit of text is read, evaluation is performed and the result is printed.
Read-Eval-Print. It's so simple, it seems almost silly to mention it, but this simplicity is a reflection of some of the deep principles underlying the language. If you truly understand these 3 operations, you will gain some deep insights into Lisp and into computation itself. Read-Eval-Print, Read-Eval-Print. Let this be your mantra when Lisp confronts you with doubts.
An implication of the REPL is that everything which can be read has a value - apart from code which causes errors - and that every value can be printed. I refer to this as value semantics2 - the meaning of a piece of text is a computation (i.e., reading + evaluation) which calculates an object (the value of the text). Complex expressions are built up from smaller expressions. In little b, the text computes data structures which represent physical entities (like molecular species) and their relationships (like reactions). One way to look at Lisp/little b code is to ask - what is the value of this expression?
Although we've focused on the command prompt, Lisp can receive its input from any source that provides a sequence of characters - for example a text file. In this case, forms are read, evaluated and printed to a well-defined location one by one until an error occurs or the sequence ends. You may regard a text file as a sequence of forms that you could have typed in at the command prompt to get the same results3.
Next, we'll look in more detail at the read/eval/print mechanism before proceeding on to actual little b code.
1That is, a form is any computational object which is intended to be evaluated. In the next section we'll see that special evaluation procedures apply when the form is a symbol or a list. All other forms - numbers, user-defined data structures, arrays - are considered self-evaluating - that is they evaluate to themselves.
2This is entirely different from what C programmers mean. In C, "value semantics", also refered to as "copy semantics", is a mechanism for passing arguments to functions.
3The meaning of code can depend on the source - e.g., the name of file - however, this is a specialized technique mostly used by debugging tools to track the location of definitions.
the read eval print loop
|