[plt-scheme] #'(require help)

From: Jos Koot (jos.koot at telefonica.net)
Date: Sat Sep 26 15:04:07 EDT 2009

Use datum->syntax for unhygienic macros:

#lang scheme

(define-syntax (define-my-id stx)
 (syntax-case stx ()
  ((_ id)
 #`(define
  #,(datum->syntax stx
     (string->symbol
      (string-append "my-id-"
       (symbol->string (syntax->datum #'id)))))
    (lambda () (display 'id))))))

(define-my-id trout)

(my-id-trout) ; --> void, displaying trout

Jos

  ----- Original Message ----- 
  From: Laurent 
  To: PLT-Scheme Mailing List 
  Sent: Saturday, September 26, 2009 8:34 PM
  Subject: [plt-scheme] #'(require help)


  Hi all,

  I timidly require your help on a simple macro I've been trying to do for some time now.

  I usually prefer to do things myself, but I'm stuck.

  Here's the thing :
  I have an identifier and I want to generate an identifier based on it but with a different name.
  (in fact I have lots of identifiers)

  for example :
  (define-my-id trout) 
  would generate the macro identifier my-id-trout that expands to, say, (display "trout").
  My concern is about using in the program an identifier that is not defined explicitly in the program file.
  This should be possible though, since this is partly what define-struct does, I suppose.


  With eval and quasi-quoting I can easily generate an approximation with a lambda'd form but Scheme won't let me use identifiers that are not explicitly defined (it works in the REPL though):
  (define (define-my-id id)
    (eval `(define ,(string->symbol (string-append "my-id-" (symbol->string id))) 
          (lambda () (display ,(symbol->string id))))))

  I also know that syntax-id-rules can generate expanding identifiers, but I can't use string-append et al in macros to generate a new id...

  Also, where can I find some simple macro samples, other than in the guide ?

  Thanks a lot,
  Laurent





------------------------------------------------------------------------------


  _________________________________________________
    For list-related administrative tasks:
    http://list.cs.brown.edu/mailman/listinfo/plt-scheme
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20090926/f550bd53/attachment.html>

Posted on the users mailing list.