230 likes | 362 Views
Lisp Control and Data Structures. CIS 479/579 Bruce R. Maxim UM-Dearborn. 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. or. > (or t nil t) t > (or 1 2 3) 1
E N D
Lisp Control and Data Structures CIS 479/579 Bruce R. Maxim UM-Dearborn
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
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
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)
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
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)
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)) . . . )
Recursive Functions mn = 1 for n = 0 mn = m * mn-1 for n > 0 > (defun expon (m n) (cond ((zerop n) 1) (t (* m (expon m (1- n))) ) ) ) expon > (expon 2 3) 8
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
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
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)
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)
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))
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)
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)
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
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
do > (defun count (L) (do ((cnt 0 (1+ cnt)) (loaf L (cdr loaf)) ) ((null loaf) cnt) ) ) count > (count '(a (b c) d e)) 4
do > (defun fact (x) (do ((n 1 (1+ n)) (res 1) ) ((> n x) res) (setq res (* res n)) ) ) fact > (fact 6) 720
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
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
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
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"