4

I want a macro that create a new identifier like

(new-name first second) => first-second

that could be used to define new toplevel bindings

(define-syntax define-generic 
  (syntax-rules ()
    ((define-generic (name a b ...))
     (begin
       (define (new-name name data) 15)      ; <= create a new binding
       (define name (lambda (a b ...)
         (add (new-name name-data) 7))))))   ; <= use new identifier

If i set! the value of the "new-name" binding, then it should affect the newly created procedure.

erjiang
  • 44,417
  • 10
  • 64
  • 100
knivil
  • 916
  • 5
  • 10
  • What are you trying to do? There is probably a better way to go about it. – C. K. Young Jan 28 '11 at 20:26
  • Hard to describe: I am working with chicken scheme and implemented an oop-system similar to coops (only simpler). Procedures can be decorated with additional data (extend-procedure). I want to emulate that in Gambit-C by creating a new binding name-data (where name is the procedure name) with define. If the user creates a new generic procedure then two new bindings according to the given name should be defined, a new procedure and a procedure-data binding. – knivil Jan 29 '11 at 14:21

2 Answers2

2

There was a discussion on Reddit on this just a few days back. Might be worthwhile studying the implementation posted for more details - http://www.reddit.com/r/scheme/comments/f54dk/i_wrote_an_hygienic_definemacro_that_can_capture/

Sid Heroor
  • 663
  • 4
  • 11
1

You can't do it in a pure R5RS. Fortunately, most of the popular Scheme implementations provides a proper macro system besides that limited R5RS hygienic stuff:

(define-macro (new-name a b) (string->symbol (string-append (symbol->string a) "-" (symbol->string b))))

SK-logic
  • 9,605
  • 1
  • 23
  • 35
  • -1 for "proper macro system" troll. Unhygienic macros are highly problematic, and even more so for Scheme because Scheme is a Lisp-1 without symbol packages, unlike Common Lisp. – C. K. Young Jan 28 '11 at 20:22
  • 1
    It's an old holy war. I personally HATE hygienic macros. They're limiting. They're stupid. I can easily implement a hygienic system on top of a PROPER (c'mon, gimme another -1), fundamental macro system, but you can't implement a fundamental system on top of a hygienic one. See the example of what is possible with a proper macro system here: http://www.meta-alternative.net/mbase.html – SK-logic Jan 28 '11 at 21:33
  • I have tried this in Gambit-C but (define (new-name a b) 5) does not work (in different variations). – knivil Jan 29 '11 at 14:29
  • @knivil, it won't work with define, let, let*, letrec and so on - only where an expression is expected. You need to define a transformer macro in order to get it working. It will help if you elaborate more on why do you need such a thing. – SK-logic Jan 29 '11 at 17:22
  • Have you read this article? http://okmij.org/ftp/Scheme/define-struct.html It can be ugly, but I think you might also be surprised at some of the things you can do with hygienic macros... – all-too-human Feb 01 '11 at 22:19
  • @all-too-human, of course you can do a lot with Scheme macros, this language is Turing-complete (as well as the C++ templates language), but the show stopper thing is that Scheme itself is not available from that language. Which means - no I/O, no string and symbols manipulation, no reflection, no access to an environment. It's just a blind external preprocessor with no connection to the actual code it is transforming. – SK-logic Feb 02 '11 at 12:09