Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn.
-
Upload
caren-parks -
Category
Documents
-
view
216 -
download
0
Transcript of 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
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
output(prin1 "sam")"sam""sam"> (princ "sam")sam"sam"> (print "sam")"sam""sam"> (terpri) nil
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
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 " "))
Plotting Output> (make-graph)FUNCTION TO GRAPH? squareSTARTING VALUE? -3ENDING VALUE? 3PLOTTING SYMBOL? umd umd umd umdumd umd umd umd " "
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))
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
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
Displaying File Contents
(do* ((fp (open "plot.lsp" :direction :input))
(ex (read fp nil) (read fp nil))
)
((null ex) nil)
(print ex)
)
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
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
Copied File is Executable> (make-graph)FUNCTION TO GRAPH? squareSTARTING VALUE? -2ENDING VALUE? 2PLOTTING SYMBOL? * * ** * * " "
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
Batch File Processing
> (batch "test.txt" "out.txt")
t
• Contents of “test.txt”(car '(a b c))
(* 2 3)
(cons 'a '(x y z))
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
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)
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)
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)
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)
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)
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
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)
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
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
Macros
> (defmacro if2 (a b c)
`(cond (,a ,b)
(t ,c)
)
)
if2
> (if2 (atom x) 'yes 'no)
yes
&optional
> (defmacro if2 (a b &optional c)
`(cond (,a ,b)
(t ,c)
)
)
if2
> (if2 (atom x) 'yes 'no)
yes
> (if2 (atom x) 'yes)
yes
&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>