Subversion Repositories programming

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
74 irasnyd 1
irasnyd@duallie lisp $ cat hw13.lisp 
2
;Written By: Ira Snyder
3
;Due Date:   03-04-2005
4
;Homework #: HW13 (Exercise #15a)
5
 
6
;;; Examples of the INC* macro input, and what it is transformed into
7
;
8
; Input:  (INC* x y z)
9
; Output: (progn
10
;           (setf z (1+ z))
11
;           (setf y (1+ y))
12
;           (setf x (1+ x))
13
;         )
14
;
15
; Input:  (INC* a)
16
; Output: (progn (setf a (1+ a)))
17
;
18
; Input:  (INC*)
19
; Output: (progn)
20
;;;
21
 
22
;;; The INC* macro takes an unlimited number of already initialized
23
;;; variables and increments them by 1. Note that variables that do not
24
;;; already have a value CAN NOT BE INCREMENTED.
25
(defmacro INC* (&rest VARS)
26
  (do ((VARS VARS (cdr VARS))
27
       (RET nil))
28
      ((null VARS) (cons 'progn RET))
29
    (setf RET (cons `(setf ,(car VARS) (1+ ,(car VARS))) RET))
30
  )
31
)
32
irasnyd@duallie lisp $ clisp -q
33
 
34
[1]> (load 'hw13.lisp)
35
;; Loading file hw13.lisp ...
36
;; Loaded file hw13.lisp
37
T
38
[2]> (setf x 0 y 1 z 3)
39
3
40
[3]> x
41
 
42
[4]> y
43
1
44
[5]> z
45
3
46
[6]> (INC* x y z)
47
1
48
[7]> x
49
1
50
[8]> y
51
2
52
[9]> z
53
4
54
[10]> (macroexpand-1 '(INC* x y z))
55
(PROGN (SETF Z (1+ Z)) (SETF Y (1+ Y)) (SETF X (1+ X))) ;
56
T
57
[11]> (setf a 0)
58
 
59
[12]> a
60
 
61
[13]> (inc* a)
62
1
63
[14]> a
64
1
65
[15]> (macroexpand-1 '(INC* a))
66
(PROGN (SETF A (1+ A))) ;
67
T
68
[16]> (INC*)
69
NIL
70
[17]> (macroexpand-1 '(INC*))
71
(PROGN) ;
72
T
73
[18]> (bye)
74
irasnyd@duallie lisp $ 
75