Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation.

Similar presentations


Presentation on theme: "Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation."— Presentation transcript:

1 Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation in memory List operators: cons, append, rplaca, rplacd.. Lists as arguments to functions

2 Lisp Lecture #2  Exam #1  Will cover Lisp programming  February ???-th, take-home Today:  Review lecture on Lisp  More on functions  More on Lisp internals  Non-destructive and destructive operations Next time:  Problem solving using search (Chapter 3 R&N)  Read chapter before coming to class

3 Basic LISP Data Types Expression Sequence Array Atom ListString NumberSymbol Integer Ratio Float 3 7/13 23.45 (has arms 2) “Rosebud” ‘Rosebud (12 7 32 78) (bob (has (arms 2) (legs 2))) Structure 4, 7, 12..Thing id: 764 loc: (12 29)..

4 Truth values = T (true) and NIL (false) AND, OR, NOT, XOR.. Everything but NIL has a truth value of T - even 0!!! (and t t) => t; (and 1 0) => t ; (or nil nil 0) => t; (xor 1 t) => nil (not t) =>nil; (not nil) => t; (not 45) => nil; *Logical operators only evaluate as many arguments (left to right) as necessary to determine the truth value of the expression. (and nil (* 88 99 66)) => nil ;; the * is never evaluated (and (* 88 99) nil) => nil ;; the * IS evaluated due to L-R eval order (not (and (or nil nil) (xor 1 0))) => t ;; the xor is never evaluated * NIL is both false and the empty list, depending upon the context Logical Operators

5 a b Dotted Pairs > (cons ‘a nil) => (a) > (cons ‘a (cons ‘b nil)) => (a b) > (cons ‘a ‘(b)) => (a b) > (cons ‘a ‘b) => (a. b) > (cons ‘(a b) ‘c) => ((a b). c) > (cons (cons ‘a ‘b) ‘c) => ((a. b). c) aab ab c a b c > (car ‘(a. b)) => a > (cdr ‘(a. b)) => b ; symbol > (cdr ‘(a b)) => (b) ; cons cell

6 Recursion count-atoms null cond

7 Optional arguments Binds to the car of list of arguments Binds to the cdr of list of arguments Optional argument (root 4)==>2 (root 4 3)==>4 1/3

8 More on Lisp Internals Storage allocation Non-destructive list operations Destructive list operations Pointer manipulation: setf, eq Efficiency considerations

9 Internal List Representation Free Storage List

10 equal eq Two pointers point to the same location

11 Garbage collection

12 Efficiency user-reverse

13 Efficiency Considerations res is optional argument Argument list

14 Variable Assignment & Access Assignment SETF (setf y (+ 3 5)) => y assigned the value 8 (setf y ‘((a 1) (b 2) (c 3))) => y assigned to the data list * Can be used to set arbitrary locations in Lisp data structures (setf (second y) 88) => 88 y => ((a 1) 88 (c 3)) SETQ (setf x (+ 3 5)) => x assigned the value 8 * First argument must be a symbol, not a general location. Access Anytime a variable appears, it will be evaluated (i.e. its value will be accessed), unless the variable appears inside the scope of a single or double quote, or is an argument to a macro. > x => 8 > ‘x => x

15 Evaluating LISP Expressions (1) Evaluate := compute/fetch value of an expression Form := an expression to be evaluated A number evaluates to itself: > 76 => 76 A variable evaluates to its value: > (setf x 54) > x => 54 A quoted symbol evaluates to the symbol itself: > ‘z => z A string evaluates to itself: > “portland” => “portland”

16 Evaluating LISP Expressions (2) A single quoted list evaluates to a simple list of symbols > ‘(a b c) => (a b c) > ‘(+ 2 3) => (+ 2 3) An unquoted list evaluates to a function call > (+ 2 3) => 5 > (a b c) => ERROR: attempt to call an undeclared function ‘a Evaluate arguments left-to-right, then call function: > (setf bob 5 bill 7 sue 9) > (+ bob bill sue) => 21 ;; (Calls + with 3 values: 5, 7, 9) Polish notation: (operator operand1 operand2 … operandn) (+ 2 3 4 5) simpler than 2 + 3 + 4 + 5 + 6 if you can live with all the parentheses!! Parentheses => no need for arithmetic precedence rules > (+ (* a b) (* c d)) -vs- a*b + c*d

17 Depth-First Evaluation Order (f1 (f2 a (f3 b)) c 8 (f2 3 2)) f1 f2 af3 b c 8f2 3 2

18 List Accessors LENGTH (length ‘(a b c)) => 3; (length nil) => 0 FIRST (or CAR) (first ‘(a b c)) => a ; (first y) => (a 1) ; (first ‘y) => Error! REST (or CDR) (rest ‘(a b c)) => (b c); (rest y) => ((b 2) (c 3)); (first (rest y)) => (b 2); (first (first (rest y))) => b NTH (nth 0 ‘(a b c)) => a ; (nth 2 ‘(a b c)) => c NTHCDR (n nested calls to REST/CDR) (nthcdr 2 y) => ((c 3)) BUTLAST (everything but the last n elements) (butlast y 2) => ((a 1)) FIRST, SECOND, THIRD...TENTH return corresponding element of the list Data: y => ((a 1) (b 2) (c 3))

19 List Constructors CONS (“construct”) (cons ‘a ‘(a b c)) => (a a b c); (cons ‘(a b) ‘(c d)) => ‘((a b) c d) (cons ‘a nil) => (a); (cons nil ‘(a b)) => (nil a b)) = (() a b) LIST (list ‘a ‘b) => (a b); (list ‘(a b) ‘c ‘d) => ((a b) c d) APPEND (append ‘(a b c) ‘(d e f)) => (a b c d e f); (append nil ‘(a b)) => (a b) These are not destructive: > (setf x ‘(a b c)) > (cons 62 x) => (62 a b c) > x => (a b c)

20 Execution of cons

21

22 Destructive List Modifiers (1) PUSH/POP: treat list like a stack and alter symbol values (“ destructive ”) > (push 77 x) => (77 a b c) > x => (77 a b c) > (pop x) => 77 > x => (a b c) REVERSE (non-destructive) (reverse ‘(a b c)) => (c b a) ; (reverse ‘((a 1) (b 2))) => ((b 2) (a 1)) NREVERSE ( destructive reverse => no copying, but use setf for safety) > (setf x '(a b c)) => (a b c) > (nreverse x) => (c b a) > x => (a) > (setf x '(a b c)) => (a b c) > (setf x (nreverse x)) => (c b a) > x => (c b a)

23 List Modifiers (2) SETF + List Accessors (Destructive!): > (setf y '(a b c)) => (a b c) > (setf (second y) 23) => 23 > y => (a 23 c) > (setf (second (cdr y)) 99) => 99 > y => (a 23 99) > (setf (cdr y) '(aaa bbb ccc)) => (aaa bbb ccc) > y => (a aaa bbb ccc)

24 Cons Cells > (setf x ‘(ntnu idi lade)) => (ntnu idi lade) > (setf y (cons ‘trondheim x)) => (trondheim ntnu idi lade) > (setf z (cons ‘(1 2 3) y)) => ( (1 2 3) trondheim ntnu idi lade) x = cons cell (standard lisp terminology) or box (Winston) trondheim y 1 32 z ntnuidilade *Each call to cons uses one new cons cell and sets car to first elem, cdr to 2nd. Shared area

25 List Creation > (setf x ‘(a b c)) ;; Uses 3 newcons cells ; same as (setf x (list ‘a ‘b ‘c)) > (setf y ‘(1 2 3)) ;; Uses 3 more new cons cells; same as (setf y (list 1 2 3)) > (setf z (list x y ‘the ‘end)) ;; Uses 4 more new cons cells abc 123 end the y x z

26 Cons cells in memory 1-byte data type 4-byte pointer to cell’s value 4-byte pointer to next cons cell Memory FFF101 FFF101: FFF252 FFF252: ntnu value: unbound idi value: unbound lade value: unbound Symbol Table AAA050:FFE073: AAA250: AAA150: AAA050 AAA150 AAA250 000000 FFF849: CCC250 FFF849 CCC250:639 Simple Data Storage *Cons cells, symbol table and regular data storage are all in memory (setf x ‘(ntnu idi lade 639)) x value: FFE073 *Each symbol has many possible properties in the symbol table

27 Non-Destructive List Operations Many beginners do this mistake They think about variables as separate “containers”

28 APPEND ntnuidilade x 123 y z > (setf x ‘(ntnu idi lade)) > (setf y ‘(1 2 3)) > (setf z (append x y)) > z (ntnu idi lade 1 2 3) > x (ntnu idi lade) *Copies all of x’s cons cells, so x is unchanged. copied shared

29 More on Destructive List Operations NCONC is destructive operation!!!!!!

30 Destructive Lisp Operations SETF is destructive operation!!!!!!

31 SETF is can cause infinite printout loop!!!!!

32 NCONC (Destructive Append) ntnuidilade x 123 y (setf x ‘(ntnu idi lade) y ‘(1 2 3)) (setf z (nconc x y)) > z (ntnu idi lade 1 2 3) > x (ntnu idi lade 1 2 3) z (nconc list1 list2…listn) - changes ‘next ptr in the last cons cells of list1 - listn-1 ‘Destructive’ function => some of the original arguments may be changed NCONC is destructive APPEND !!!!!

33 RPLACA = (SETF (FIRST..)) SETF can make destructive modifications to lists. > (setf x ‘(a b c)) > (setf y x) > (setf (first y) 99) > y (99 b c) > x (99 b c) > (setf (nth 2 y) 88) > x (99 b 88) > (rplaca y 55) > x (55 b 88) a b c x y 9988 55 RPLACA is destructive !!!!!

34 RPLACD = (SETF (REST..)) > (setf x ‘(a b c)) > (setf y x) > (setf (rest y) ‘(22 33)) > x (a 22 33) > (rplacd y ‘(77 88)) > x (a 77 88) a bc x y 2233 7788 * There are no remaining pointers to (b c) nor (22 33). Their cons cells should be freed up for cons and list to reuse. This is, they should be garbage collected. LISP has automatic garbage collection. RPLACD is destructive !!!!!

35 List Parameters in Function Calls (1) LISP uses “call by value” for all parameters. In the case of lists, this means that the formal parameters get a copy of the pointer to the list, not a copy of the whole list. (defun change1 (list) (setf (first list) (first (last list)))) (defun change2 (list) (cons (first (last list)) (rest list))) abc x value: change1 list: change 2 list: > (setf x ‘(a b c))

36 abc List Parameters in Function Calls (2) x value: > (setf x ‘(a b c)) > (change1 x) * Change1 uses setf(first..), which is destructive. So x changes also. c change1 list: SETF is destructive !!!!! (defun change1 (list) (setf (first list) (first (last list)))) (defun change2 (list) (cons (first (last list)) (rest list)))

37 abc x value: change2 list: c List Parameters in Function Calls (3) > (setf x ‘(a b c)) > (change2 x) * Change2 uses cons, which is non-destructive. So x is unchanged. (defun change1 (list) (setf (first list) (first (last list)))) (defun change2 (list) (cons (first (last list)) (rest list))) CONS is not destructive !!!!!


Download ppt "Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation."

Similar presentations


Ads by Google