@@ -1048,6 +1048,309 @@ to stop it from loading redundant code which is already in the saved image.
\chapter{Understanding Common Lisp}
\label{chap:understandingcommonlisp}
Gendl is a superset of Common Lisp, and is embedded in Common
Lisp. This means that when working with Gendl, you have the full power
of CL available to you. The lowest-level expressions in a Gendl
definition are CL ``symbolic expressions,'' or ``s-expressions.''
This chapter will familiarize you with CL s-expressions.
\section{S-expression Fundamentals}
\label{sec:s-expressionfundamentals}
S-expressions can be used in your definitions to establish
the value of a particular \emph{slot} in an object, which will be computed on-demand. You can also
evaluate S-expressions at the toplevel \texttt{gdl-user\textgreater} prompt, and see the result immediately. In fact, this toplevel prompt is called a \emph{read-eval-print} loop, because its purpose is to \emph{read} each s-expression entered, \emph{evaluate} the expression to yield a result (or \emph{return-value}), and finally to \emph{print} that result.
CL s-expressions use a \emph{prefix} notation, which means that they consist of either an \emph{atom} (e.g. number, text string, symbol) or a list (one or more
items enclosed by parentheses, where the first item is
taken as a symbol which names an operator). Here is an example:
\begin{verbatim}(+ 2 2)
\end{verbatim}This expression consists of the function named by the symbol \texttt{+}, followed by the numeric arguments \texttt{2} and another \texttt{2}. As you may have guessed, when this expression is evaluated it will return the value 4.\emph{Try it: }try typing this expression at your command prompt, and see
the return-value being printed on the console. What is actually
happening here? When CL is asked to evaluate an expression, it
processes the expression according to the following rules:
\begin{itemize}
\item If the expression is an \emph{atom} (e.g. a non-list datatype such as a number, text
string, or literal symbol), it simply evaluates to itself. Examples:
\begin{itemize}
\end{itemize}
Note that numbers are represented directly (with decimal
points and slashes for fractions allowed), strings are surrounded by
double-quotes, and literal symbols are introduced with a preceding
single-quote. Symbols are allowed to have dashes (``-'') and most
other special characters. By convention, the dash is used as a word
separator in CL symbols.
\item If the expression is a list (i.e. is surrounded by
parentheses), CL processes the \emph{first} element in this list as an \emph{operator name}, and the \emph{rest} of the elements in the list represent the \emph{arguments} to the operator. An operator can take zero or more
arguments, and can return zero or more return-values. Some operators
evaluate their arguments immediately and work directly on those
values (these are called \emph{functions}. Other operators expand into other code. These are called \emph{special operators} or \emph{macros}. Macros are what give Lisp (and CL in particular) its
special power. Here are some examples of functional s-expressions:
\begin{itemize}
\item
\begin{verbatim}gdl-user> (expt 2 5)
32
\end{verbatim}
\item
\begin{verbatim}gdl-user> (+ 2 5)
7
\end{verbatim}
\item
\begin{verbatim}gdl-user> (+ 2)
2
\end{verbatim}
\item
\begin{verbatim}gdl-user> (+ (+ 2 2) (+ 3 3 ))
10
\end{verbatim}
\end{itemize}
\end{itemize}
\section{Fundamental CL Data Types}
\label{sec:fundamentalcldatatypes}
As we have seen, Common Lisp natively supports
many data types common to other languages, such as numbers and text
strings. CL also contains several compound data types such as lists,
arrays, and hash tables. CL contains \emph{symbols} as well, which typically are used as names for other data elements.
Regarding data types, CL follows a paradigm called dynamic
typing. Basically this means that values have type, but variables do
not necessarily have type, and typically variables are not
``pre-declared'' to be of a particular type.
\subsection{Numbers}
\label{subsec:numbers}
As we have seen, numbers in CL are a native
data type which simply evaluate to themselves when entered at the
toplevel or included in an expression.
Numbers in CL form a hierarchy of types, which includes Integers,
Ratios, Floating Point, and Complex numbers. For many purposes, you
only need to think of a value as a ``number'' without getting any more
specific than that. Most arithmetic operations, such as \texttt{+}, \texttt{-}, \texttt{*}, \texttt{/} etc, will automaticaly do any necessary type coercion on their
arguments and will return a number of the appropriate type.
CL supports a full range of floating-point decimal numbers, as well as
true Ratios, which means that \texttt{1/3} is a true one-third, not \texttt{0.333333333} rounded off at some arbitrary precision.
\subsection{Strings}
\label{subsec:strings}
Strings are actually a specialized kind of
array, namely a one-dimensional array (vector) made up of text
characters. These characters can be letters, numbers, or punctuation,
and in some cases can include characters from international character
sets (e.g. Unicode or UTF-8) such as Chinese Hanzi or Japanese
Kanji. The string delimiter in CL is the double-quote character.
As we have seen, strings in CL are a native data type which simply
evaluate to themselves when included in an expression.
\subsection{Symbols}
\label{subsec:symbols}
Symbols are such an important data structure in CL, that people
sometimes refer to CL as a ``Symbolic Computing Language.'' Symbols
are a type of CL object which provides your program with a built-in
mechanism to store and retrieve values and functions, as well as being
useful in their own right. A symbol is most often known by its name
(actually a string), but in fact there is much more to a symbol than
its name. In addition to the name, symbols also contain a \emph{function} slot, a \emph{value} slot, and an open-ended \emph{property-list} slot in which you can store an arbitrary number of named properties.
For a named function such as \texttt{+} the function-slot contains the actual function
object for performing numeric addition. The value-slot of a symbol can
contain any value, allowing the symbol to act as a global variable, or \emph{parameter}. And the property-list, also known as the \emph{plist} slot, can contain an arbitrary amount of information.
This separation of the symbol data structure into function, value, and
plist slots is one obvious distinction between Common Lisp and most
other Lisp dialects. Most other dialects allow only one (1) ``thing''
to be stored in the symbol data structure, other than its name
(e.g. either a function or a value, but not both at the same
time). Because Common Lisp does not impose this restriction, it is not
necessary to contrive names, for example for your variables, to avoid
conflicting with existing ``reserved words'' in the system. For
example, \texttt{list} is the name of a built-in function in CL. But you
may freely use \texttt{list} as a variable name as well. There is no need to
contrive arbitrary abbreviations such as \texttt{lst}.
How symbols are evaluated depends on where they occur in an
expression. As we have seen, if a symbol appears first in a list
expression, as with the \texttt{+} in \texttt{(+ 2 2)}, the symbol is evaluated for its function slot. If the first
element of an expression indeed has a function in its function slot, then any
subsequent symbol in the expression is taken as a variable, and it is evaluated
for its global or local value, depending on its scope (more on variables and
scope later).
As noted in Section 3.1.3, if you want a literal symbol itself, one
way to achieve this is to ``quote'' the symbol name:
\begin{verbatim}'a
\end{verbatim}
Another way is for the symbol to appear within a quoted list expression, for example:
\begin{verbatim}'(a b c)
\end{verbatim} or
\begin{verbatim}'(a (b c) d)
\end{verbatim}
Note that the quote (\texttt{'}) applies across everything in the list expression, including any sub- expressions.
\subsection{Lists}
\label{subsec:lists}
Lisp takes its name from its strong support for the list data
structure. The list concept is important to CL for more than this
reason alone --- most notably, lists are important because \emph{all CL programs are themselves lists.}
Having the list as a native data structure, as well as the form of all
programs, means that it is straightforward for CL programs to compute
and generate other CL programs. Likewise, CL programs can read and
manipulate other CL programs in a natural manner. This cannot be said
of most other languages, and is one of the primary distinguishing
characteristics of Lisp as a language.
Textually, a list is defined as zero or more elements surrounded by
parentheses. The elements can be objects of any valid CL data types,
such as numbers, strings, symbols, lists, or other kinds of
objects. As we have seen, you must quote a literal list to evaluate it
or CL will assume you are calling a function. Now look at the