使用宏创建新标识符
我想要一个宏来创建一个新的标识符,这样
(new-name first second) => first-second
可以用来定义新的顶级绑定
(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
如果我设置了! “new-name”绑定的值,那么它应该影响新创建的过程。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
几天前,Reddit 上对此进行了讨论。可能值得研究发布的实现以获取更多详细信息 - http://www.reddit .com/r/scheme/comments/f54dk/i_wrote_an_hygienic_definemacro_that_can_capture/
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/
你无法在纯 R5RS 中做到这一点。幸运的是,除了有限的 R5RS 卫生的东西之外,大多数流行的方案实现都提供了适当的宏系统:
(define-macro (new-name ab) (string->symbol (string-append (symbol->string a ) "-" (符号->字符串 b))))
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))))