Introduction to LISP Atoms, Lists Math
LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical functions –mapping from one domain to another
LISP Basics n Common objects are atoms and lists –one thing is both n Atoms as in Prolog – a thing in itself n Lists as in Prolog – a sequence of things n Syntax is different, tho’
Interacting with LISP n You write something down n LISP tells you its value –it’s an error if it has no value n What you write down is an atom or a list n What you get back is an atom or a list
Evaluating Atoms n When you give something to LISP, it evaluates it n Some things evaluate to “themselves” –numbers, for example > n Most things do not – more later
Math in LISP n LISP evaluates mathematical expressions –math functions use their common symbols n LISP uses prefix notation –operator comes before the operands n Math expression in parentheses > ( ) 33
Math Expressions as Lists n Lists set off in parentheses –no commas between elements n Operator is first element of the list n Operands come after –can actually have multiple operands > ( ) 70
Math Expressions as Operands n Operands can be math expressions > (+ 23 (* 2 5)) 33 n Expression is evaluated & result used in place of the expression –multiply 2 by 5 to get 10 –add 23 and 10 to get 33
Exercise n Evaluate these expressions: 57 ( ) (* ) (+ (* 2 5) (/ 6 3) (– 6 1)) (+ ( ) (* 2 5 7) (* 2 (+ 2 1))) (/ )
Function Calls n Function calls are lists –function name first element of list –arguments come after –whole in (parentheses) – not [brackets] –no commas – elements separated by spaces n Just like with math –function goes inside the parentheses
Function Calls n Some functions are built in… > (sin 3.14) > (max –9 87 5) 88 n …and you can create your own (more later) > (factorial 4) 24
Very Common Mistake n Function name goes inside parentheses –always always always (except…) –even when it’s a name –even when inside other parentheses > (+ max(3 10) min(10 8)) Error: …MAX… (also min) > (+ (4 * 5) (7 – 3)) Error: …4… (also 7)
Case Sensitivity n LISP has none –it’s an old language – late 1950s n Everything gets mapped to upper case –well, almost everything n Doesn’t matter which way you write things –max, MAX, Max, and mAx all the same: MAX –no atom/variable distinction
Atoms/Variables n Atoms can have values > pi d0 > (sIn Pi) d-16 n LISP variables like imperative variables –value can be changed –except some declared constant
Evaluating Atoms n Normal atoms start with no value –asking for their value is an error > mark Error: … unbound variable `MARK'. n To get thing itself back, put a single quote in front of it > ‘mark MARK Note: one single quote at the front; no “closing” quotation mark
Assigning Values to Atoms n Various set functions > (set ‘comp2043 ‘mark) MARK > comp2043 MARK > (setf comp2043 ‘mark) MARK Text uses SETF instead of SET same but first argument not quoted known as a “special form” Set returns the value it assigned just like = in c
Using Atoms with Values n Atom just gets replaced with its value –unless you quote it, of course > comp2043> ‘comp2043 MARKCOMP2043 > (set ‘comp2113 comp2043) MARK > comp2113 MARK Unquoted atom (comp2043) is replaced by its value (mark)
Exercise: What Values? > (set ‘x 10) > x > ‘x > (set ‘y 50) > (+ x y) > (set ‘v ‘x) > (set v 20) > (+ x y)
Argument Evaluation > comp2043 MARK > (set comp2043 ‘newval) NEWVAL > comp2043 MARK > mark NEWVAL n All arguments get evaluated –except special forms n Quotation mark inhibits evaluation –‘ is a special form
Set and SetF n Set is actually deprecated –in danger of going away n Use setf instead (short for “set field”) n Setf is a special form –means some arguments not evaluated –lots of these in LISP – gotta just remember > (setf mark ‘newval) (set ‘mark ‘newval) first argument of setf not evaluated
More Atoms n Atoms in LISP are pretty much any string –some punctuation OK! –no spaces, commas, semi-colons, colons > ‘all-that&a-bag-of-chips! ALL-THAT&A-BAG-OF-CHIPS! > ‘23a+b 23A+B
Values of Values n Taking a value only goes one step –value of x is Y, value of y is Z value of x is Y > (setf x ‘y) Y > (setf y ‘z) Z > x Y It doesn’t say Z here because x’s value isn’t Z
Lists as Data n Lists can be arguments to functions > (setf mark ‘(comp2043 comp2113)) (COMP2043 COMP2113) n Comp2043 and comp2113 are not evaluated –part of a quoted list n Value of atom MARK is a list of two atoms –each of those atoms has a value –but those values are not MARK’s values
Quoted Lists n Single quote mark applies to the whole list –not treated as a function at all –even if it looks like a function > ‘(setf x y) (SETF X Y) Note: quote is in front of the list the list is not evaluated nor any of its “arguments”
The Empty List n The empty list has no elements –parentheses with nothing in them (spaces OK) n Evaluates to NIL –nil is just another name for the empty list > ( ) NIL n Can quote or not – doesn’t matter > ‘( ) NIL
Exercises n What values do the following assign? (setf shortlist ‘( )) shortlist = ? (setf sum1 ( )) sum1 = ? (setf sum2 ‘( )) sum2 = ?
Working with Lists n Lists are a big part of LISP n Need to be able to: –take lists apart –put lists together n Functions for doing just that
Taking Lists Apart n Primary functions for splitting lists: –first returns first element of a list –rest returns everything but first element of list > (first ‘(a b c d)) A > (rest ‘(a b c d)) (B C D)
Removing from Lists n First & rest do not change a variable’s value > (setf old ‘(b c d)) (B C D) > (first old) B > (rest old) (C D) > old (B C D) old’s value remains same
Second, Third & Last n Common LISP provides second, third, …, tenth as built-in functions > (third ‘(a b c d)) C n Last gives list with just last element in it > (last ‘(a b c d)) (D)
Lists in Lists n List may have another list as an element > ‘((a b) (c d) (e f)) ((A B) (C D) (E F)) n First, second, &c return top-level elements > (first ‘((a b) (c d) (e f))) (A B) > (rest ‘((a b) (c d) (e f))) ((C D) (E F))
Elements That Aren’t There n Ask for tenth element of list with only four –not an error – returns nil instead > (tenth ‘(a b c d)) NIL n First & rest similarly > (rest ()) NIL
CAR and CDR n CAR & CDR are same as first & rest –old-fashioned function names n CADR is the CAR of the CDR –the first of the rest = the second –similarly CADDR = third n CDDR = CDR of CDR = all but 1 st & 2 nd n Can combine in weird ways, too
Exercise n Evaluate the following: (first ‘( )) (rest ‘( )) (first (rest ‘( ))) (third ‘( )) (first ‘(rest ( ))) (cddar ‘((1 2 3) (4 5 6) (7 8 9)))
Putting Lists Together n Append combines two lists into one –works for more than two, too > (append ‘(a b c) ‘(d e f)) (A B C D E F) > (append ‘(1 2 3) ‘(a b c) ‘(do re mi)) (1 2 3 A B C DO RE MI) > (append () () () () () ‘(a) () () () () ‘(b)) (A B)
Putting Lists Together n List makes a list out of its arguments –as many as you give it > (list ‘a ‘b ‘c ‘(d e f) ‘g) (A B C (D E F) G) > (list (* 1 1) (* 2 2) (* 3 3) (* 4 4)) ( )
Adding to the Front of a List n Cons puts an element on the front of a list > (cons ‘a ‘(b c)) (A B C) > (setf old ‘(b c d)) (B C D) > (cons ‘a old) (A B C D) > old (B C D) Note: cons returns a new list old value not changed
Making Lists n Make multiple lists into one: –append n Make a list out of various things –list n Put one element at front of a list –cons n None of them change variable values!
Exercise n What function is used to: –make ( ) from (1 2 3) and (4 5)? –make ( ) from 1 and ( )? –make ((1 2 3) (4 5)) from (1 2 3) and (4 5)? –make ((1 2 3) 4 5) from (1 2 3) and (4 5)? –make (1 2 3 (4 5)) from 1, 2, 3 and (4 5)?
Adding to the Front of a List (II) n Use push and pop to modify a variable > (setf old ‘(b c d)) (B C D) > (push ‘a old) (A B C D) > old (A B C D) > (pop old) A > old (B C D)
Length of a List n Length returns the length of a list > (length ‘(a b c d e)) 5 n Returns top-level length > (length ‘((a b) (c d) (e f (g) h i))) 3
Exercise n Evaluate the following (list (append ‘(1 2) ‘(3 4)) (cons ‘a ‘(b c d)) (cons ‘a ‘(b c d)) (length ‘(a s d f)) (length ‘(a s d f)) (rest (append ‘(4 5 6) (cons 1 ‘(2 3))))) (rest (append ‘(4 5 6) (cons 1 ‘(2 3)))))
Next Time n Defining functions in LISP –Chapters 3 & 4