CS W4701 Artificial Intelligence: Fall 2013 Lisp Crash Course
CS W4701 Artificial Intelligence: Fall 2013 Lisp Crash Course
Artificial Intelligence
Fall 2013
Lisp Crash Course
Jonathan Voris
(based on slides by Sal Stolfo)
Another Quick History Lesson
1956: John McCarthy organizes Dartmouth AI conference
Wants a list processing language for AI work
Experiments with Advice Talker
1958: MarCarthy invents LISP
LISt Processor
1960: McCarthy publishes Lisp Design
Recursive Functions of Symbolic Expressions and Their
Computation by Machine, Part I
Implemented by Steve Russel
eval in machine code
1962: First compilers by Tim Hart and Mike Levin
Another Quick History Lesson
Afterwards, tons of variant Lisp projects
Stanford LISP
ZetaLisp
Franz Lisp
PSL
MACLISP
NIL
LML
InterLisp
SpiceLisp
AutoLisp
Scheme
Clojure
Emacs Lisp
Another Quick History Lesson
1981: DARPA sponsors meeting regarding
splintering
Several projects teamed up to define Common
Lisp
Common Lisp is a loose Language specification
Many implementations
Such as LispWorks
1986: Technical working group formed to draft
ANSI Common Lisp standard
1994: ANSI INCITS 226-1994 (R2004)
Why Lisp?
Freedom
Very powerful, easily extensible language
Development Speed
Well suited for prototyping
Politics
McCarthy liked it, so should you
Symbolic
Homoiconic: code structures are the same as data
structures (lists!)
The Big Idea
Everything is an expression
Specifically, a Symbolic or S-expression
Nested lists combining code and/or data
Recursively defined as:
An atom, or
A list (a . b) where a and b are s-expressions
A Note on Syntax
Youll usually see (a b c)
Where are the dots?
(a b c) is a shortcut for (a . (b . (c . NIL)))
Data
Atoms (symbols) including numbers
All types of numbers including Roman! (well, in the
early days)
Syntactically any identifier of alphanumerics
Think of as a pointer to a property list
Immutable, can only be compared, but also serve as
names of variables when used as a variable
Lists are the primary data object
There are others
Arrays, Structures, Strings (ignore for now)
S-expressions are interpreted list structures
Data
Atoms (symbols) including numbers
All types of numbers including Roman! (well, in the
early days)
Syntactically any identifier of alphanumerics
Think of as a pointer to a property list
Immutable, can only be compared, but also serve as
names of variables when used as a variable
Lists are the primary data object
There are others
Arrays, Structures, Strings (ignore for now)
S-expressions are interpreted list structures
Functions
Defined using the defun macro
(defun name (parameter*)
"Optional documentation string."
body-form*)
Hello World
(defun hello ()
(print "hello world)
)
Programs
Series of function definitions (there are many
built-in functions)
Series of function calls
Read/Eval/Print
(Setf In (Read stdio))
(Setf Out (Eval In))
(Print Out)
In other words (Loop (Print (Eval (Read))))
Singly linked Lists
A cons cell has a First field (CAR) and a Rest
field (CDR)
X
car cdr
A
B
(Setf X `(A B C))
C
() = nil = empty list = FALSE
Nil is a symbol, and a list and its value is false.
List Manipulation Funcs
Car, First
(Car (Car (Car L)))
Cdr, Rest
(Car (Cdr (Cdr L)))
Cons
(Cons 1 nil) (1)
(Cons 1 `(2)) (1 2)
car and cdr: Whats in a Name
Metasyntatic? Arbitrary? Foreign?
Russel implemented Lisp on IBM 704
Hardware support for special 36 bit memory treatment
Address
Decrement
Prefix
Tag
car: Contents of the Address part of the Register
number
cdr: Contents of the Decrement part of the Register
number
cons: reassembled memory word
List Manipulation Functions
List
(List 1 2 3) (1 2 3)
Quote,
Dont evaluate arguments, return them
(Quote (1 2)) = `(1 2) = (1 2) as a list with two elements
Otherwise 1 better be a function!
List vs quote: List does not stop evaluation
Listp
Push, Pop
Append
Remove
Member
Length
Eval
Arithmetic
The usual suspects:
Plus +
Difference
Times *
Divide /
Incf
Decf
Functional Composition
Prefix notation
aka Cambridge prefix notation
aka Cambridge Polish notation
(f (g (a (h t))) f( g( a, h(t)))
Predicates
Atom
(Atom `(A)) is false, i.e. nil, because (A) is a list, not an atom
(Atom `A) is true, i.e. 1 or T
(Atom A) is either, depending upon its value! A here is regarded as a
variable
Numberp
Null
(Null `(1)) is nil
(Null nil) is T
Zerop
And/Or/Not
(And A B C) = T if the value of all of the variables are non-nil
(Or A B C) = the value of the first one that is non-nil, otherwise nil
Property Lists Association Lists
Lisp symbols have associated property list
structures
Atom a has property p with value v
A computing context consists of a set of
variables and their current values
( (key1 val1) (key2 val2))
Hmmm.
The Special Expression let
let defines local variables
(let ( (var1 val) (var2 val) )
*body* )
Similar to:
(Setf Name (lambda(variables) *body*)