lisp control and data structures n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Lisp Control and Data Structures PowerPoint Presentation
Download Presentation
Lisp Control and Data Structures

Loading in 2 Seconds...

play fullscreen
1 / 23

Lisp Control and Data Structures - PowerPoint PPT Presentation


  • 109 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Lisp Control and Data Structures' - tamah


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
lisp control and data structures

Lisp Control and Data Structures

CIS 479/579

Bruce R. Maxim

UM-Dearborn

slide2
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

slide3
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

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

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

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

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

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

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

slide18
do

> (defun count (L)

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

(loaf L (cdr loaf))

)

((null loaf) cnt)

)

)

count

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

4

slide19
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 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
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 lists1
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
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"