[CSUSB]
>> [CNS]
>> [Comp Sci Dept]
>> [R J Botting]
>> [CSci620]
>>
15
[Source]
Now I'm suspicious of the assoc on page 182. This is
most odd. To get the behavior described in the book
you need this
[ p182.lsp ]
code. To get the behavior in the predefined XLISP assoc
then you use the code on page 182. More in the lab!
Oddly the LISP2.0 manual(Steele) defines the behavior of
assoc in accordance with the book's code, not the author's
ideas.
Property Lists
The XLISP property list functions are different.
In XLISP we have:
The code on page 185 is also way too long... [ p184.lsp ] is shorter. It is also unrealistic: only one parent per child?
You can (TRACE F1 F2 F3) to start tracing calls of these three functions,
and then (UNTRACE F2) to stop the tracing of F2.
What is setq and set?
setq is the equivalent of an assignment statement. After
(setq V E) the value of E is stored as the value of V.
set is more powerful. In (set VP E) both VP and E are evaluated.
If VP has an atomic value, say V, then that variable, V, is changed
to have the value of E.
What is setf?
I don't know.
It means: set field.
How does a consp differ from an atom?
consp is a test to see if something is a cons. A cons
is a constructed pair. Atoms are not constructed but are
a single piece and can not be disassembled with car and cons.
On page 184 the book says that "null" is returned, but I get "NIL"!
The "null" is in quotes and is a metaphor meaning "nothing".
In LISP this is shown as NIL.
Why wasn't T removed from LISP?
Many millions of lines of code use T as true. All this
code would need fixing if T was removed. It will be part
of LISP for a long time to come.
How does one differ from (one)
one is an atom but (one) is a list with a single element.
(one) is a dotted pair with car=one and cdr=NIL.
Does our LISP have put?
No. It has putprop.
Why doesn't put work on my CLisp?
On many LISPs the function is putprop not put. The syntax is
different:
If (car A) = B then for some C, A = (cons B C).
So
If (cdr A) = B then for some C, A = (cons C B)
So
So
So
Here is a test using XLISP and presetting variables v,w,y,z to be them selves:
> (setq x (cons (cons W (cons Y Z)) V) )
> (cadar x)
Y
What does eval do?
The function eval evaluates an argument twice!
In an expression (eval x), first x is evaluated by the interpreter. Then the result is re-evaluated again.
Try this in our XLISP:
>(setq e '(+ 1 2)); e is an expression
(+ 1 2)
> e
(+ 1 2)
> (eval e); evaluate e
3
> (eval (list '* 2 3))
6
> (setq f '(* 2 3))
(* 2 3)
> (setq g (list '+ e f)); construct a complex list out of e and f
(+ (+ 1 2) (* 2 3))
> (eval g)
9
Page 182 Why does (assoc m 2) =two?
It doesn't. It should return (2 two)!
Why do we ever cons something with an empty list?
We put a NIL at the end of a list as a sentinel.
It is a null
pointer! A list like this (A B C D .... Z) has a series of
cars A B C.... until you get to the end with a NIL.
This includes a list with one element like (A)!
(A) = (A . NIL)
(A B) = (A . (B.NIL))
(A B C) = (A . (B. (C . NIL)))
The list function does a very good job of creating these lists in programs:
m = "((add +) (minus -))";but this doesn't express the structure of the right hand side.
Perhaps something like this:
m = cons( cons("add", "+"), cons("minus", "-") );
except that we don't have a list function in C++. However,
using the STL pair class:
class List;
typedef pair<List*, List*> * List;
List * cons( List* x, List* y){ return new pair(x, new pair(y, NULL));}
is getting close to doing the right thing in C++, but needs
garbage collection and some other (nasty) details sorted out.
However the only real way to map LISP into C++ is write a LISP data type
in C++. For example, somewhere in
[ http://www.csci.csusb.edu/dick/cs320/lisp/src/ ]
are the C functions for interpreting LISP expressions. This
is the code for our XLISP.
If assoc is a mapping why isn't it called map?
The name goes back to the original LISP1.5 at least. I don't
know why McCarthy chose it. I do know that map is a different
function in LISP that we don;t have time to cover.
If assoc doesn't find a key what does it return?
NIL.
Book's arguments for assoc are in a different order to XLISP and the handout?
The handout was taken directly from the source code and documentation
for XLISP. The books version is different:
I think that XLISP follows the standard LISP and the book does not.
Is it OK to omit a ' in front of a number (page 183)?
Yes. All the numbers are recognized and return as a value a
binary number representing them.
. . . . . . . . . ( end of section Questions) <<Contents | Index>>
Laboratory
Some simple LISP functions
[ lab15.html ]
Next
Semantics of LISP Chapter 7 185 onward
[ 16.html ]
(outline).
. . . . . . . . . ( end of section CS620 Session 15 LISP 102) <<Contents | Index>>
Glossary