1 / 28

Lisp Files, Arrays, and Macros

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

mai
Download Presentation

Lisp Files, Arrays, and Macros

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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

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

  4. output > (defun out () (print "sam")(prin1 "sam")(princ "sam") ) out > (out) "sam" "sam"sam "sam" > (progn (princ "sam") (terpri)) sam nil > (progn (princ "sam") t) sam t

  5. 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 " "))

  6. Plotting Output > (make-graph) FUNCTION TO GRAPH? square STARTING VALUE? -3 ENDING VALUE? 3 PLOTTING SYMBOL? umd umd umd umd umd umd umd umd " "

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

  8. 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

  9. 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

  10. Displaying File Contents (do* ((fp (open "plot.lsp" :direction :input)) (ex (read fp nil) (read fp nil)) ) ((null ex) nil) (print ex) )

  11. 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

  12. 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

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

  14. 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

  15. Batch File Processing > (batch "test.txt" "out.txt") t • Contents of “test.txt” (car '(a b c)) (* 2 3) (cons 'a '(x y z))

  16. 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

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

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

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

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

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

  22. 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

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

  24. 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

  25. 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 > x 3 > y 4

  26. Macros > (defmacro if2 (a b c) `(cond (,a ,b) (t ,c) ) ) if2 > (if2 (atom x) 'yes 'no) yes

  27. &optional > (defmacro if2 (a b &optional c) `(cond (,a ,b) (t ,c) ) ) if2 > (if2 (atom x) 'yes 'no) yes > (if2 (atom x) 'yes) yes

  28. &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 >

More Related