%\documentstyle[11pt]{article}
%\begin{document}
\section[PSL Hints]{PSL Hints\footnote{This tutorial
    was written for Common Lisp by Geoffrey J. Gordon,  February 5,
    1993 and
    modified by Victor Ufnarovski for PSL}}




\subsection{Symbols}

A symbol is just a string of characters. There are restrictions on what
you can include in a symbol and what the first character can be, but as
long as you stick to letters, digits, and hyphens, you'll be safe.
(Except that if you use only digits and possibly an initial hyphen,
LISP will think you typed an integer rather than a symbol.) Some
examples of symbols:

\begin{verbatim}
        a
        b
        c1
        foo
        bar
        baaz-quux-garply
\end{verbatim}

Some   things  you  can do   with  symbols follow.    (Things  after a
``\verb+>+''  prompt are what you type  to the LISP interpreter, while
other things are  what the  LISP  interpreter prints back  to you. The
``\verb+%+''  is   LISP's    comment  character:  everything from    a
``\verb+%+'' to the end of line is ignored.)

\begin{verbatim}
1 lisp>  (setq a 5)       %store a number as the value of a symbol
5
2 lisp>   a                %take the value of a symbol
5
3 lisp>  (PLUS2 a 6) %use the value of a symbol as an argument to a function
11
4 lisp>   b         %try to take the value of a symbol which has no value
***** `b' is an unbound ID
5 lisp>  (+ a 6) % even usual + might not work
***** `iplus2' is an undefined function
6 lisp> (copyd '+ 'PLUS2) % but you can create it, copiying definitions

Do you really want to redefine the system function `!+'? (Y or N)

?Y
*** Function `!+' has been redefined
!+
7 lisp> (+ a 6)
11


\end{verbatim}

In the sequel  for the briefety we skip number and 'lisp' from the
prompt,
remaining \verb+>+ only.

There are two  special symbols, {\tt  t} and {\tt  nil}.  The value of
{\tt t} is defined always to be {\tt t}, and the value of {\tt nil} is
defined always to be {\tt  nil}. LISP uses {\tt  t}  and {\tt nil}  to
represent true  and false.   An example of  this   use is in   the
conditional
statement, described more fully later:

\begin{verbatim}
> (cond (nil 5) (t 6) )
6
> (cond  (4 5) (t 6) )
5
\end{verbatim}

The last example is odd but correct: {\tt nil} means false, and anything else
means true. (Unless we have a reason to do otherwise, we use {\tt t} to mean
true, just for the sake of clarity).

Symbols like {\tt t} and {\tt nil} are called self-evaluating symbols, because
they evaluate to themselves.


\subsection{Numbers}

An  integer is a  string of  digits  optionally preceded by {\tt+}  or
\verb+-+.  A real  number looks like an  integer, except that it has a
decimal point and optionally can be written  in scientific notation.
A number is any of the above. Here are some numbers:

\begin{verbatim}
        5
        17
        -34
        +6
        3.1415
        1.722e-15
       
\end{verbatim}

The standard arithmetic functions are  available but not alawys in
the usual notations:
\begin {itemize}
\item {\tt PLUS} instead of {\tt +};
\item {\tt MINUS} instead of unary
\verb+-+ and {\tt DIFFERENCE} instead of bynary.
\item {\tt TIMES }instead of \verb+*+,
\item  {\tt QUOTIENT} instead of \verb+/+,
\item {\tt LESSP} instead of \verb+<+, (P here means Predicate)
\item  {\tt floor}, {\tt ceiling},  {\tt sin},
{\tt  cos},  {\tt tan}, {\tt  sqrt},  {\tt  exp},  {\tt  expt},
and so
forth.
\end{itemize}
All of  them accept any kind of  number as an argument.  {\tt PLUS},
 {\tt DIFFERENCE}, {\tt TIMES } and {\tt QUOTIENT}
 return a   number according to  type
contagion: e.g. an integer plus a real is a real. Here are some
examples:

\begin{verbatim}
> (PLUS 2 2.3)
4.3
> (DIFFERENCE 4 5)    
-1
> (MINUS 4 5) %second parameter ignored!
-4
> (floor 2.3)
2
> (ceiling 2.3)
3
> (exp 1)               %e
2.71828 
> (exp 3)               %e*e*e
20.0855 
> (expt 3 4.2)          %exponent with a base other than e
100.904
> (PLUS 5 6 7 (TIMES 8 9 10))  %the fns +-*/ accept multiple arguments
738
\end{verbatim}

There is no limit to the absolute value of an integer except the memory
size of your computer. Be warned that computations with bignums (as
large integers are called) can be slow. (So can computations with
rationals, especially compared to the corresponding computations with
small integers or floats.)



\subsection{Conses}

A cons (or a dotted pair) is just a two-field record.
The fields are called {\it car} and
{\it  cdr}, for historical reasons. (On   the first machine where LISP
was implemented, there  were two instructions {\tt  car} and {\tt cdr}
which stood for {\it contents of address register} and {\it contents of
decrement  register}.    Conses were   implemented   using  these  two
registers.)

Conses are easy to use:

\begin{verbatim}
> (cons 4 5) %Allocate a cons. Set the car to 4 and the cdr to 5.
(4 . 5)
> (cons (cons 4 5) 6)
((4 . 5) . 6)
> (car (cons 4 5))
4
> (cdr (cons 4 5))
5
\end{verbatim}



\subsection{Lists}

You can build many structures out of conses. Perhaps the simplest is a
linked list: the car of each  cons points to one of the elements of the
list, and the  cdr points either to another  cons or to {\tt nil}. You can
create such a linked list with the {\tt list} fuction:

\begin{verbatim}
> (list 4 5 6)
(4 5 6)
\end{verbatim}

Notice that LISP prints linked lists a special way: it omits some of
the periods and parentheses. The rule is: if the cdr of a  cons is {\tt nil},
LISP doesn't bother to print the period or the {\tt nil}; and if the  cdr of
 consA is  cons B, then LISP doesn't bother to print the period for cons
A or the parentheses for cons B. So:

\begin{verbatim}
> (cons 4 nil)
(4)
> (cons 4 (cons 5 6))
(4 5 . 6)
> (cons 4 (cons 5 (cons 6 nil)))
(4 5 6)
\end{verbatim}

The  last example is  exactly equivalent to   the call
\begin{verbatim}(list 4 5 6) \end{verbatim}. Note that {\tt nil} now means the  list with no elements: the cdr
of \begin{verbatim}(a b)\end{verbatim}, a list with 2 elements, is\begin{verbatim}(b)\end{verbatim},  a list with 1
element% and   the cdr  of \begin{verbatim}  (b)\end{verbatim},  a list  with  1   element, is
\begin{verbatim} nil \end{verbatim}, which therefore must be a list with no elements.

The car and cdr of {\tt nil} are defined to be {\tt nil}.

You can store your list in a variable:

\begin{verbatim}
> (setq a nil)
NIL
> (setq  a (list 1 2 3) )
(1 2 3)

\end{verbatim}



\subsection{Functions}

You saw one example of a function above. Here are some more:

\begin{verbatim}
> (PLUS 3 4 5 6)      %this function takes any number of arguments
18
> (PLUS (PLUS 3 4) (PLUS (PLUS 4 5) 6))     %isn't prefix notation fun?
22
> (de foo (x y) (PLUS x y 5))   %defining a function
foo
> (foo 5 0)                     %calling a function
10

Here is a recursive definition of the factorial:

70 lisp> (de fact (x)               %a recursive function
    (cond ((LESSP 0 x) 
           (TIMES x (fact (DIFFERENCE x 1))))
          (T 1)
  ) )70 lisp> 70 lisp> 70 lisp> 70 lisp> 
fact
71 lisp> (fact 5)
120

1 lisp>  (de bar (x)      %a function with multiple statements in
    (setq x (TIMES x 3))   %its body -- it will return the value
    (setq x (DIFFERENCE x 2))    %returned by its final statement
1 lisp> 1 lisp> 1 lisp>     (PLUS x 4)
  )1 lisp> 
bar
2 lisp> (bar 6)
20


\end{verbatim}

When we defined {\tt foo}, we gave it  two arguments, {\tt x} and {\tt
y}. Now when we call {\tt foo}, we are required to provide exactly two
arguments: the first will become the value of {\tt x} for the duration
of the call to {\tt foo}, and the second will become the value of {\tt
y} for the duration of the call. In LISP, most variables are lexically
scoped; that is, if  {\tt foo} calls {\tt  bar} and {\tt bar} tries to
reference {\tt  x}, {\tt bar} will not  get {\tt foo}'s value for {\tt
x}.

The process of assigning a symbol a value for the duration of some
lexical scope is called binding.

--------------------- part 2 to be corrected  after that-----------------------
