Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lisp Control and Data Structures CIS 479/579 Bruce R. Maxim UM-Dearborn.

Similar presentations


Presentation on theme: "Lisp Control and Data Structures CIS 479/579 Bruce R. Maxim UM-Dearborn."— Presentation transcript:

1 Lisp Control and Data Structures CIS 479/579 Bruce R. Maxim UM-Dearborn

2 and (and nil t t) nil > (and t nil t) nil > (and 1 2 3) 3 > (defun sign (a b) (and (oddp a) (oddp b) 'both-odd) ) sign > (sign 2 3) nil > (sign 3 5) both-odd

3 or > (or t nil t) t > (or 1 2 3) 1 > (or 'george nil 'harry) george > (defun same-sign (x y) (or (and (zerop x) (zerop y)) (and (< x 0) (< y 0)) (and (> x 0) (> y 0)) ) same-sign > (same-sign 0 0) t > (same-sign -1 3) nil

4 cond > (defun comp (op x y) (cond ((equal op 'sum-of) (+ x y)) ((equal op 'prod-of) (* x y)) (t '(does not compute)) ) comp > (comp 'sum-of 2 4) 6 > (comp 'larry 2 3) (does not compute)

5 if and case > (if (listp 1) (car 1) '(not a list) ) (not a list) > (setq b 'c) c > (case B ('a '1st) ('b '2nd) ('c '3rd) ) 3rd

6 let > (defun aug (first second) (let ((item first) (bag second) ) (cond ((listp first) (setq item second) (setq bag first)) ) (if (member item bag) bag (cons item bag)) ) aug > (aug 'a '(a b c)) (a b c) > (aug '(a b c) 'a) (a b c)

7 let* Please remember let does not allow you to declare local identifiers with initial values that depend on one another You need to use let* for these types of declarations (let* ((item (if (listp first) second first)) (bag (if (= item second) first second))... )

8 Recursive Functions m n = 1 for n = 0 m n = m * m n-1 for n > 0 > (defun expon (m n) (cond ((zerop n) 1) (t (* m (expon m (1- n))) ) expon > (expon 2 3) 8

9 car/cdr recursion > (defun cnt-atom (l) (cond ((null l) 0) ; empty list ((atom l) 1) ; not a list (t (+ (cnt-atom (car l)) (cnt-atom (cdr l)) ) cnt-atom > (cnt-atom '(a (b c) d (e f (g h)))) 8

10 trace and untrace > (trace cnt-atom) (cnt-atom) > (cnt-atom '(a b)) Entering: CNT-ATOM, Argument list: ((a b)) Entering: CNT-ATOM, Argument list: (a) Exiting: CNT-ATOM, Value: 1 Entering: CNT-ATOM, Argument list: ((b)) Entering: CNT-ATOM, Argument list: (b) Exiting: CNT-ATOM, Value: 1 Entering: CNT-ATOM, Argument list: (nil) Exiting: CNT-ATOM, Value: 0 Exiting: CNT-ATOM, Value: 1 Exiting: CNT-ATOM, Value: 2 2 > (untrace cnt-atom) nil

11 apply > (apply '+ '(2 3 4 5)) 14 > (+ 2 3 4 5) 14 > (apply 'equal '(12 14)) nil > (apply 'cons '(as (you like it))) (as you like it)

12 mapcar > (mapcar 'oddp '(1 2 3 4)) (t nil t nil) > (defun square (x) (* x x) ) square > (mapcar 'square '(1 2 3 4 5)) (1 4 9 16 25)

13 mapcar > (setq words '((one eins) (two zwei) (three drei)) ) ((one eins) (two zwei) (three drei) > (mapcar 'car words) (one two three) > (mapcar 'cadr words) (eins zwei drei) > (mapcar 'cdr words) ((eins) (zwei) (drei))

14 other map functions > (mapcar 'reverse words) ((eins one) (zwei two) (drei three)) > (mapcan 'reverse words) (eins one zwei two drei three) > (maplist 'reverse words) (((three drei) (two zwei) (one eins)) ((three drei) (two zwei)) ((three drei))) > (mapcon 'reverse words) ((three drei) (two zwei) (one eins) (three drei) (two zwei) (three drei)) > (maplist 'cdr words) (((two zwei) (three drei)) ((three drei)) nil)

15 lambda functions > (mapcar #'square '(1 2 3 4 5)) (1 4 9 16 25) > (mapcar #'(lambda (x) (* x x)) '(1 2 3 4 5)) (1 4 9 16 25) > (mapcar #'(lambda (x) (car x) (cadr x)) words) (eins zwei drei) > (mapcan #'(lambda (x) x) words) (one eins two zwei three drei)

16 prog and loops > (defun expt (m n) (prog ((result 1) (expon n)) loop1 (if (zerop expon) (return result)) (setq result (* m result)) (setq expon (1- expon)) (go loop1) ) expt > (expt 2 5) 32

17 progn and prog1 > (progn (setq x 'foo) (setq x 'bar) (setq x 'baz) 'done) done > x baz > (prog1 (setq x 'foo) (setq x 'bar) (setq x 'baz) 'done) foo > x baz

18 do > (defun count (L) (do ((cnt 0 (1+ cnt)) (loaf L (cdr loaf)) ) ((null loaf) cnt) ) count > (count '(a (b c) d e)) 4

19 do > (defun fact (x) (do ((n 1 (1+ n)) (res 1) ) ((> n x) res) (setq res (* res n)) ) fact > (fact 6) 720

20 dolist and dotimes > (dolist (x '(a b c) y) (setq y (list x)) ) (c) > (dotimes (x 3 x) x) 3 > (dotimes (x 3 x) (print x) ) 0 1 2 3

21 property lists > (putprop 'fred 'male 'sex) male > (get 'fred 'sex) male > (get 'fred 'height) nil > (setf (get 'fred 'sex) 'female) female > (get 'fred 'sex) female

22 property lists > (setf (symbol-plist 'fred) '(sex male age 23 sibs (bob carol))) (sex male age 23 sibs (bob carol)) > (get 'fred 'sibs) (bob carol) > (remprop 'fred 'sibs) nil > (symbol-plist 'fred) (sex male age 23) > (setq fred 10) 10 > fred 10 > (symbol-plist 'a) nil

23 strings > (char "sam eats soup" 5) #\a > (string 97) "a > (string #\a) "a" > (char "SAM EATS SOUP" 5) #\A > (strcat "a" "b" "c") "abc" > #\a #\a > (subseq "sam eats soup" 3 5) " e"


Download ppt "Lisp Control and Data Structures CIS 479/579 Bruce R. Maxim UM-Dearborn."

Similar presentations


Ads by Google