Thank you both, that is exactly what I need! <br>With the syntax-id-rules and I also use a (certainly common) macro pattern, which I just learned today (from planet btw) to apply it a list of values :<br>(define-syntax-rule (define-my-ids stx ...)<br>
(begin (define-my-id stx) ...))<br><br>This last one is the kind of macro I'd be glad to find in the PLT docs.<br><br>Thanks again,<br>Laurent<br><br><br><div class="gmail_quote">On Sat, Sep 26, 2009 at 20:49, Jon Rafkind <span dir="ltr"><<a href="mailto:rafkind@cs.utah.edu">rafkind@cs.utah.edu</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div class="im">Laurent wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi all,<br>
<br>
I timidly require your help on a simple macro I've been trying to do for<br>
some time now.<br>
<br>
I usually prefer to do things myself, but I'm stuck.<br>
<br>
Here's the thing :<br>
I have an identifier and I want to generate an identifier based on it but<br>
with a different name.<br>
(in fact I have lots of identifiers)<br>
<br>
for example :<br>
(define-my-id trout)<br>
would generate the macro identifier my-id-trout that expands to, say,<br>
(display "trout").<br>
My concern is about using in the program an identifier that is not defined<br>
explicitly in the program file.<br>
This should be possible though, since this is partly what define-struct<br>
does, I suppose.<br>
<br>
<br>
</blockquote>
<br></div>
Maybe something like this is what you are after<br>
<br>
#lang scheme<br>
<br>
(define-syntax (define-my-id stx)<br>
(syntax-case stx ()<br>
[(_ id)<br>
(with-syntax ([my-id (datum->syntax<br>
#'id<div class="im"><br>
(string->symbol<br>
(string-append "my-id-"<br>
(symbol->string<br></div>
(syntax->datum #'id))))<br>
#'id)])<br>
#'(define-syntax-rule (my-id) (display 'id)))]))<br>
<br>
(define-my-id foo)<br>
(my-id-foo)<br>
;; end<br>
<br>
In general if you want to create new identifiers you should use syntax-case and with-syntax. with-syntax takes some syntax and binds it to an identifier that can be used in a template and you are free to generate the syntax that it binds. define-syntax-rule is just a shorthand for (define-syntax foo (syntax-rules ...)).<div class="im">
<br>
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
With eval and quasi-quoting I can easily generate an approximation with a<br>
lambda'd form but Scheme won't let me use identifiers that are not<br>
explicitly defined (it works in the REPL though):<br>
(define (define-my-id id)<br>
(eval `(define ,(string->symbol (string-append "my-id-" (symbol->string<br>
id)))<br>
(lambda () (display ,(symbol->string id))))))<br>
<br>
I also know that syntax-id-rules can generate expanding identifiers, but I<br>
can't use string-append et al in macros to generate a new id...<br>
<br>
<br>
</blockquote>
<br></div>
If you want to use the result of the above macro as 'my-id-foo' instead of '(my-id-foo)' then you can use syntax-id-rules, but using eval is usually not the right way to generate new identifiers.<div class="im">
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Also, where can I find some simple macro samples, other than in the guide ?<br>
<br>
<br>
</blockquote>
<br></div>
I guess you can look at code on planet or in the collects directory of the PLT tree, but I'm beginning to think a more expansive explanation section of the docs would be useful. Something like extremely detailed comments about "real code".<br>
</blockquote></div><br>