This is the mail archive of the guile@sourceware.cygnus.com mailing list for the Guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

inc!/dec! macros (Was: Syntatic sugar and identifier permissivity)


Marius Vollmer writes:
 > You want
 > 
 >     (inc! (accessor (some-expensive-operation)))
 > 
 > to expand into something like
 > 
 >     (let ((arg1 (some-expensive-operation)))
 >       (set! (accessor arg1) (+ (accessor arg1) 1)))

(defmacro inc! (place . val)
  (let ((val (if (pair? val) (car val) 1)))
    (if (pair? place)
        (if (null? (cddr place))
            (let ((eva (gensym)))
              `((lambda (,eva)
                  (set! (,(car place) ,eva)
                        (+ ,val (,(car place) ,eva))))
                ,(cadr place)))
            (let ((eva (map (lambda (i) (gensym)) (cdr place))))
              `((lambda ,eva
                  (set! (,(car place) ,@eva)
                        (+ ,val (,(car place) ,@eva))))
                ,@(cdr place))))
        `(set! ,place (+ ,val ,place)))))

- and I don't even know if defmacro is the preferred user interface to 
macros. Anyway, dec! is left as an exercise for the interested reader.

	Ole

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]