Lisp
No difference between code and data
- List Processing language
- Uses S-Expressions
- Symbols like variables, but can be used if not defined
- Incremental compilation
- Prefix notation
Anything after a colon, ;, is a comment.
Quotation
- QUOTE - a function to return value of expression without evaluating it.
- Shorthand is ‘a <==> (quote a)
- Returns A
Equality
(equalityPredicate X Y)
When in doubt use EQL. EQ isn’t guaranteed to compare numbers
or characters (since it’s left up to the individual implementation).
EQUAL compares strings and lists (recursively), and is overkill in
most situations.
Cons cells
- Basic building block of lisp data structures.
- Represented by (A . B)
- We don’t normally use dot expressions because S-exp is more useful.
- (A . (B . (C))) <==> (A B C)
- CAR = Contents of Address Register
- CDR = Contents of Decrement Register
Accessing lists
- CAR - returns the first element of a list
- CDR - returns the rest of a list
- (car ‘(1 2 3)) => 1
- (cdr ‘(1 2 3)) => (2 3)
Building lists
- CONS - create a cons cell with the two arguments
- LIST - accepts any number of args a creates a list of them
Predicates
- NULL - Return true if OBJECT is a NULL, and NIL otherwise.
- ATOM - Return true if OBJECT is an ATOM, and NIL otherwise.
- LISTP - Return true if OBJECT is a LIST, and NIL otherwise.
Arithmatic
+, -, /, *, >, <
Special Operators
COND is written using IF in many implementations.
Defining Functions
Append
- Construct a new list by concatenating the list arguments
- (cons ‘(1 2) ‘(3 4)) => (1 2 (3 4))
- (list ‘(1 2) ‘(3 4)) => ((1 2) (3 4))
- (append ‘(1 2) ‘(3 4)) => (1 2 3 4)
Writing Append
Since this is recursive
- Terminating condition
- Otherwise…
Append fn definition
(defun appendr (first second)
"Return a list with the concatenation of the first list to the second"
(cond
((null first) second)
(t (cons (car first) (appendr (cdr first) second)))))
Functions to try writing
- LAST-ELEMENT - Returns the last element in a list.
- DEPTH - Consider a list as a tree. Find the depth of the tree.
- SUBSTITUTE - Return a sequence of the same kind as SEQUENCE with the same elements,
except that all elements equal to OLD are replaced with NEW.