lisp control and data structures cis 479/579 bruce r. maxim um-dearborn

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

Upload: rachel-zimmerman

Post on 28-Mar-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

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

Lisp Control and Data Structures

CIS 479/579

Bruce R. Maxim

UM-Dearborn

Page 2: 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

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

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

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

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)

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

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

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

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)

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

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

. . .

)

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

Recursive Functions

mn = 1 for n = 0mn = 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

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

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

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

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

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

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)

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

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)

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

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

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

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)

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

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)

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

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

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

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

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

do

> (defun count (L)

(do ((cnt 0 (1+ cnt))

(loaf L (cdr loaf))

)

((null loaf) cnt)

)

)

count

> (count '(a (b c) d e))

4

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

do> (defun fact (x)

(do ((n 1 (1+ n))

(res 1)

)

((> n x) res)

(setq res (* res n))

)

)

fact

> (fact 6)

720

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

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

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

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

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

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> fred10> (symbol-plist 'a)nil

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

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"