开发者

Create new identifier with macros

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 affe开发者_StackOverflowct the newly created procedure.


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/


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜