Rev 74 | Blame | Compare with Previous | Last modification | View Log | RSS feed
irasnyd@duallie lisp $ cat hw13.lisp
;Written By: Ira Snyder
;Due Date: 03-04-2005
;Homework #: HW13 (Exercise #15a)
;;; Examples of the INC* macro input, and what it is transformed into
;
; Input: (INC* x y z)
; Output: (progn
; (setf z (1+ z))
; (setf y (1+ y))
; (setf x (1+ x))
; )
;
; Input: (INC* a)
; Output: (progn (setf a (1+ a)))
;
; Input: (INC*)
; Output: (progn)
;;;
;;; The INC* macro takes an unlimited number of already initialized
;;; variables and increments them by 1. Note that variables that do not
;;; already have a value CAN NOT BE INCREMENTED.
(defmacro INC* (&rest VARS)
(do ((VARS VARS (cdr VARS))
(RET nil))
((null VARS) (cons 'progn RET))
(setf RET (cons `(setf ,(car VARS) (1+ ,(car VARS))) RET))
)
)
irasnyd@duallie lisp $ clisp -q
[1]> (load 'hw13.lisp)
;; Loading file hw13.lisp ...
;; Loaded file hw13.lisp
T
[2]> (setf x 0 y 1 z 3)
3
[3]> x
0
[4]> y
1
[5]> z
3
[6]> (INC* x y z)
1
[7]> x
1
[8]> y
2
[9]> z
4
[10]> (macroexpand-1 '(INC* x y z))
(PROGN (SETF Z (1+ Z)) (SETF Y (1+ Y)) (SETF X (1+ X))) ;
T
[11]> (setf a 0)
0
[12]> a
0
[13]> (inc* a)
1
[14]> a
1
[15]> (macroexpand-1 '(INC* a))
(PROGN (SETF A (1+ A))) ;
T
[16]> (INC*)
NIL
[17]> (macroexpand-1 '(INC*))
(PROGN) ;
T
[18]> (bye)
irasnyd@duallie lisp $