Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.

Similar presentations


Presentation on theme: "Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical."— Presentation transcript:

1 Introduction to LISP Atoms, Lists Math

2 LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical functions –mapping from one domain to another

3 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’

4 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

5 Evaluating Atoms n When you give something to LISP, it evaluates it n Some things evaluate to “themselves” –numbers, for example > 23 23 n Most things do not – more later

6 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 > (+ 23 10) 33

7 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 > (+ 23 10 7 30) 70

8 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

9 Exercise n Evaluate these expressions: 57 (+ 3 4 5 6) (* 1 2 3 4) (+ (* 2 5) (/ 6 3) (– 6 1)) (+ (+ 10 10 10) (* 2 5 7) (* 2 (+ 2 1))) (/ 36 6 2)

10 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

11 Function Calls n Some functions are built in… > (sin 3.14) 0.001592548 > (max 2 4 9 88 3 –9 87 5) 88 n …and you can create your own (more later) > (factorial 4) 24

12 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)

13 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

14 Atoms/Variables n Atoms can have values > pi 3.141592653589793d0 > (sIn Pi) 1.2246063538223773d-16 n LISP variables like imperative variables –value can be changed –except some declared constant

15 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

16 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

17 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)

18 Exercise: What Values? > (set ‘x 10) > x > ‘x > (set ‘y 50) > (+ x y) > (set ‘v ‘x) > (set v 20) > (+ x y)

19 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

20 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

21 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

22 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

23 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

24 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”

25 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

26 Exercises n What values do the following assign? (setf shortlist ‘(1 2 3 4)) shortlist = ? (setf sum1 (+ 3 7 10 24)) sum1 = ? (setf sum2 ‘(+ 2 5 9 13)) sum2 = ?

27 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

28 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)

29 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

30 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)

31 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))

32 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

33 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

34 Exercise n Evaluate the following: (first ‘(10 20 30)) (rest ‘(10 20 30)) (first (rest ‘(10 20 30))) (third ‘(20 40 60 75)) (first ‘(rest (10 20 30))) (cddar ‘((1 2 3) (4 5 6) (7 8 9)))

35 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)

36 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)) (1 4 9 16)

37 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

38 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!

39 Exercise n What function is used to: –make (1 2 3 4 5) from (1 2 3) and (4 5)? –make (1 2 3 4 5) from 1 and (2 3 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)? –make (1 2 3 (4 5)) from 1, 2, 3 and (4 5)?

40 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)

41 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

42 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)))))

43 Next Time n Defining functions in LISP –Chapters 3 & 4


Download ppt "Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical."

Similar presentations


Ads by Google