Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn.

28
Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn

Transcript of Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn.

Page 1: Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn.

Lisp Files, Arrays, and Macros

CIS 479/579

Bruce R. Maxim

UM-Dearborn

Page 2: Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn.

input

• “read” is equivalent to “cin” in C++• Reads an entire expression from the

keyboard when called• Typical use

(setq A (read))

• Generally wise to prompt the user for input first, since “read” gives no indication that it is looking for input

Page 3: Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn.

output(prin1 "sam")"sam""sam"> (princ "sam")sam"sam"> (print "sam")"sam""sam"> (terpri) nil

Page 4: Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn.

output> (defun out () (print "sam")(prin1 "sam")(princ "sam") )out> (out)"sam""sam"sam"sam"> (progn (princ "sam") (terpri))samnil> (progn (princ "sam") t)samt

Page 5: Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn.

User Interaction

(defun make-graph () (princ "function to graph? ") (setq func (read)) (princ "starting value? ") (setq start (read)) (princ "ending value? ") (setq end (read)) (princ "plotting symbol? ") (setq sym (read)) (plot-points sym (mapcar func (generate start end))) (princ " "))

Page 6: Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn.

Plotting Output> (make-graph)FUNCTION TO GRAPH? squareSTARTING VALUE? -3ENDING VALUE? 3PLOTTING SYMBOL? umd umd umd umdumd umd umd umd " "

Page 7: Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn.

Plotting Example (defun space (n)

(cond ((< n 0) (princ "error!")) ((zerop n) nil) (t (princ " ") (space (1- n)))))

(defun plot-one-point (sym val) (space val) (princ sym) (terpri))

(defun plot-points (sym ylis) (mapcar #'(lambda (y) (plot-one-point sym y)) ylis))

(defun generate (m n) (cond ((> m n) nil) (t (cons m (generate (1+ m) n)))))

(defun square (x) (* x x))

Page 8: Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn.

format

> (format t "the symbol ~s appeared ~s times ~%" 'a 3)

the symbol a appeared 3 times

nil

> (format t "~% Hello ~a old boy" "bruce")

 

Hello bruce old boy

nil

> (format t "~% Hello ~a old boy" '(a b c))

 

Hello (a b c) old boy

nil

Page 9: Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn.

File Input

> (setq fp (open "plot.lsp" :direction :input))#<Character-Input-Stream 13:"a:\plot.lsp">> (read fp nil)(defun space (n) (cond ((< n 0) (princ "ERROR!"))

((zerop n) nil) (t (princ " ") (space (1- n)))))

> (setq a (read fp nil))") (SPACE (1- N)))))

> (close fp)t

Page 10: Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn.

Displaying File Contents

(do* ((fp (open "plot.lsp" :direction :input))

(ex (read fp nil) (read fp nil))

)

((null ex) nil)

(print ex)

)

Page 11: Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn.

plot.lsp Contents

(defun space (n) (cond ((< n 0) (princ "ERROR!")) ((zerop n) nil) (t (princ " ") (space (1- n)))))

(defun plot-one-point (sym val) (space val) (princ sym) (terpri))

(defun plot-points (sym ylis) (mapcar (function (lambda (y) (plot-one-point sym y))) ylis))

(defun generate (m n) (cond ((> m n) nil) (t (cons m (generate (1+ m) n)))))

(defun square (x) (* x x))(defun make-graph nil (princ "FUNCTION TO GRAPH? ")

(setq func (read)) (princ "STARTING VALUE? ") (setq start (read)) (princ "ENDING VALUE? ") (setq end (read)) (princ "PLOTTING SYMBOL? ") (setq sym (read)) (plot-points sym (mapcar func (generate start end))) (princ " "))

nil

Page 12: Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn.

File Copy

defun copy (fn1 fn2) (do* ((old (open fn1 :direction :input)) (new (open fn2 :direction :output)) (ex (read old nil) (read old nil)) ) ((null ex) (close old) (close new)) (print ex new) ))copy> (copy "plot.lsp" "temp.txt")t

Page 13: Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn.

Copied File is Executable> (make-graph)FUNCTION TO GRAPH? squareSTARTING VALUE? -2ENDING VALUE? 2PLOTTING SYMBOL? * * ** * * " "

Page 14: Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn.

Batch File Processing> (defun batch (fn1 fn2) (do* ((old (open fn1 :direction :input)) (new (open fn2 :direction :output)) (ex (read old nil) (read old nil)) ) ((null ex) (close old) (princ "end of program" new) (terpri new) (close new) ) (print ex new) (print (eval ex) new) (terpri new) ))batch

Page 15: Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn.

Batch File Processing

> (batch "test.txt" "out.txt")

t

• Contents of “test.txt”(car '(a b c))

(* 2 3)

(cons 'a '(x y z))

Page 16: Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn.

Batch File Processing• Contents of “out.txt”

(car (quote (a b c)))a (* 2 3)6 (cons (quote a) (quote (x y z)))(a x y z) end of program

Page 17: Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn.

List Surgery

> (setq a '(a b c))(a b c)> (setq b '(d e f))(d e f)> (append a b)(a b c d e f)> (nconc a b)(a b c d e f)> a(a b c d e f)> b(d e f)

Page 18: Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn.

List Surgery

(setq abc '(a b c))

(a b c)

> (setq xyz '(x y z))

(x y z)

> (setq bc (cdr abc))

(b c)

> (setq yz (cdr xyz))

(y z)

> (setq a (nconc abc xyz))

(a b c x y z)

Page 19: Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn.

List Surgery(> a(a b c x y z)> abc(a b c x y z)> xyz(x y z)> bc(b c x y z)> yz(y z)

Page 20: Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn.

List Surgery

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

(a b c d e)

> (rplaca a '(a b))

((a b) b c d e)

> a

((a b) b c d e)

> (rplacd a '(x y z))

((a b) x y z)

Page 21: Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn.

List Surgery> (setq a '(a b c d e f))(a b c d e f)> (delete 'a a)(b c d e f)> a(a b c d e f)> (delete 'e a)(a b c d f)> a(a b c d f)> (setq a (delete 'a a))(b c d f)> a(b c d f)> (delete 'a '(a b a c a d a e))(b c d e)

Page 22: Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn.

Arrays

> (setq arr (make-array 3))

#(nil nil nil)

> (setf (aref arr 2) 4)

4

> arr

#(nil nil 4)

> (aref arr 1)

nil

> (setq a 3)

3

Page 23: Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn.

Backquote> '(if a is true (and a a) then)(if a is true (and a a) then)> `(if ,a is true ,(and a a ) then)(if 3 is true 3 then)> (list 'if a 'is 'true (list a a) 'is

'true)(if 3 is true (3 3) is true)> (list 'if a 'is 'true (and a a) 'is

'true)(if 3 is true 3 is true)> (setq b '(a b c))(a b c)> `(hello fred ,b)(hello fred (a b c))> `(hello fred ,@b)(hello fred a b c)

Page 24: Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn.

Macros

> (defun name (x) (car x))name> (name '(a red table))a> (defmacro name (x) `(car ,x))name> (name '(a red table))a> (defmacro name (x) (list 'car x))name> (name '(a red table))a

Page 25: Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn.

Macros> (defmacro sq (var val) `(setq ,var ,val))sq> (defun sq-test (a b) (sq x a) (sq y b) )sq-test> (sq-test 3 4)4> x3> y4

Page 26: Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn.

Macros

> (defmacro if2 (a b c)

`(cond (,a ,b)

(t ,c)

)

)

if2

> (if2 (atom x) 'yes 'no)

yes

Page 27: Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn.

&optional

> (defmacro if2 (a b &optional c)

`(cond (,a ,b)

(t ,c)

)

)

if2

> (if2 (atom x) 'yes 'no)

yes

> (if2 (atom x) 'yes)

yes

Page 28: Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn.

&rest> (defun list2 (&rest x) x) list2> (list2 1 2 3 4 5)(1 2 3 4 5)> (defmacro let*2 (x &rest forms) (if (null x) `(progn ,@forms) `(let (,(car x)) (let*2 ,(cdr x) ,@forms) ) ) )let*2>