Numbers, booleans, symbols, characters, strings
1
2.0
#t
abc
#\a
"hello"
A series of elements surrounded by parenthesis
(a b c "easy as" 1 2 3)
Lists may contain any S-expression
An S-expression is just an atom or a list
Everything in LISP is an S-expression
(i s n t (t h i s) (c () () l?))
(define (factorial n)
(if (zero? n)
1
(* n (factorial (- n 1)))))
A cons is just a pair of values
(setf x (cons 'b null))
(setf x (cons 'a (cons 'b null)))
(setf x '(a (r t) b))
eval
interprets and evaluates an S-expression
(eval '(+ 1 2 3)) ;; 6
(eval '(a b c)) ;; Crash -- a is undefined
(+ 1 2 3) ;; 6
(- 1 2 3) ;; -4
(* (+ 1 2) (- 5 3)) ;; 6
Function arguments are evaluted in order
quote
How to prevent evalutation of an expression?
(setf x (a b c)) ;; CRASH -- a is undefined
(setf x (quote (a b c))) ;; Aha, much better
quote
does not evaluate its argument
quote
is a primitive
Single quotes are a shorthand for
quote
(setf x '(a b c)) ;; Even better yet
car
cdr
cons
car
(car '(a b c)) ;; 'a
(car '((a b c) d e f)) ;; '(a b c)
(car '()) ;; Oh noes -- CRASH
cdr
(cdr '(a b c)) ;; '(b c)
(cdr '((a b c) d e f)) ;; '(d e f)
(cdr '((a b c))) ;; '()
(cdr '()) ;; Oh noes -- CRASH
cons
(cons 'a '()) ;; '(a)
(cons 'a '(b)) ;; '(a b)
(cons '(a b) (cons 'c '())) ;; '((a b) c)
(cons 'a 'b) ;; '(a . b)