COMMON LISP - standard
CLOS - COMMON LISP Object System
Part One: symbol manipulation and basic LISP
Part Two: advanced LISP
Part Three: programs using LISP - AI programming techniques
computers use bits - binary digits : 0 &1
to represent numbers
to represent characters -> words -> sentences
groups of atoms - lists
just as: words form sentences --- sentences form paragraphs
atoms and lists are called symbolic expressions --- s-expressions
Structures:
objects: block/university
attribute/value pairs and identification
An aside: Suddenly this is "new" with XML and structure capabilities for HTML for "automated" reading. Ahhh, AI coming back
*) known objects - provide information
*) unknown: use rules to generate new information
LISP prompt:
If you do "something wrong" and a number in square brackets
shows up before the prompt
comments: begin line with a semicolon ;
LISP is
functional (procedure arguments),
i.e, a list with
elements
the first is always the function to be performed
(prefix)
and interpretive (vs compiled ...)
you type:
LISP responds:
SETF: Set Field
REMOVE, CONS construct
Procedures:
Terms:
names for primitives can be single characters: +, -, /, *
Numbers, t, nil (empty list or false) evaluate to themselves* (+ (* 2 2) (/ 2 2)) (old LISPs (plus 2 2) 5 * 8 8
pg 14, 515
nested lists examples
write expressions for
FIRST (first '(a b d)) = A
(first '((a d) c (d b)))
REST (rest '(a b d)) = (B D)
(rest '(a (c d)))
QUOTE tells LISP how far evaluation process should go
NIL ()
' is a macro character for Quote, i.e. 'A becomes (QUOTE A)
pg 20 : (2-2, 2-3, 2-4, do some)
CAR (non-destructive) (CADR CADDR second and third elements)
CDR
(CADDR '(a b c)) = (CAR (CDR (CDR '(a b c))))
= (FIRST (REST (REST '(a b c))))
SETF - binds (setf L '(a f)) (what if not quote?)
* L
LISP looks up value for atom L
* (setf ab-list '(a b))
* ab-list
*'ab-list
* (first 'ab-list)
* (first ab-list)
* (setf ab-list '(a b) (LISP returns last setf) xy-list '(x y)) (for odd number of arguments won't eval)FIRST and REST (CAR and CDR) take apart
CONS takes an expression and a list and returns a new
list with the expression as the first element
CONS is like the inverse of FIRST with REST
(non-destructive)
CONSing requires storage and is expensive . It builds a completely new list => new storage
LISP dynamically allocates storage as need arises. When it runs out of room, LISP garbage-collects unused cons cells and re-uses them. Garbage-collection is computational and time expensive.
CAR - single microsecond
CONS - with garbage-collection - millisecond
moral: if possible, manipulate rather than build
APPEND combines the elements of two (or more) lists
Note that it runs the elements of the lists together
LIST makes a list out of its arguments
pg 26, 27
NTHCDR trims off n elements from the first argument
(note: CDR (REST) trims off 1 from the first)
BUTLAST trims off the last n elements
LAST returns a list with all but the last element of the list element trimmed. (like FIRST except get a list) how can one get simply the last element? also expensive
Add an element to the end - no complement to CONS
LENGTH: counts the number of top-level elements
REVERSE: reverses the order of top-level elements (pg 31)
ASSOC: association list - consists of a list of sublists
first element is key; second element is value
* (in object-oriented programming, sarah might be an object and the keys are class or instance values)
Retrieve values by (ASSOC
Numbers: (numeric atoms)
MAX
(assoc 'weight sarah) evaluates to (WEIGHT 4.4)(note sublist)
ROUND: (round (/ 22 7)) returns (toward the even when midway) if two floating points, returns floating point
if one float then all convert to float
if two integers, returns a ratio (/ 22 7) returns 22/7
(unless divide evenly)
(float (/ 22 7)) returns 3.14286
3 :the nearest integer
1/7 : the remainder
MIN
EXPT
SQRT: give a negative, returns a complex #, data type
ABS