Question

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.

Was it helpful?

Solution

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))))

OTHER TIPS

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/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top