[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Chicken-users] how to use define-macro to...
From: |
Alex Sandro Queiroz e Silva |
Subject: |
Re: [Chicken-users] how to use define-macro to... |
Date: |
Thu, 20 Dec 2007 09:16:57 -0300 |
User-agent: |
Thunderbird 2.0.0.9 (Windows/20071031) |
Hallo,
minh thu escreveu:
Hi,
1/
I'd like to "repeat" this code for a lot of operators (not just + as
in the following).
How can I do that ?
(define old+ +)
(define (+ a b)
(if (or (instance-of? a <behavior>) (instance-of? b <behavior>))
(create-behavior (list a b) old+)
(old+ a b)))
The problem is how to capture the old + (as old+) to use it in the redefinition.
(define-macro (add-behavior op)
(let ((old (gensym))
(new (gensym)))
`(let ((,old ,op)
(,new (lambda (a b)
(if (or (instance-of? a <behavior>) (instance-of? b
<behavior>))
(create-behavior (list a b) ,old)
(,old a b)))))
(set! ,op ,new))))
2/
I could write the following at any place I want to print debug info.
(cond-expand
(debug-behaviors (printf "Debugging...~N"))
(else)
Note the particular symbol debug-behaviors.
But it's boring; I would prefer have a "debug" macro instead :
(debug
(printf "Debugging...~N"))
But I don't know how I can make it expends to nothing as in the else
clause of the
cond-expand. I use this :
(define-macro (debug rest)
(cond-expand
(debug-behaviors rest)
(else '(noop))))
I have to use the noop procedure.
Also, can I write a "higher" macro to generate the "debug" one with a
particular debug-something symbol ?
(cond-expand
(debug-behaviors (define debugging-behaviors? #t))
(else (define debugging-behaviors? #f)))
(define-macro (debug . args)
(when debugging-behaviors?
`(apply print ,args)))
I hope this helps, it's not tested :-)
Cheers,
-alex