在Guile中,我正在尝试学习define-syntax和syntax-rules。我的印象是,方案宏不评估它们的论点,但我的宏似乎是这样做的:
(define-syntax foo
(syntax-rules ()
((foo word)
(if (eqv? word 'bar) (begin (write "Caller said BAR!") (newline)))
)))如果我用(foo bar)调用它,我会得到错误消息
未绑定变量: bar
然而,如果我用(foo 'bar)调用它,我就得到了预期的
“呼叫者说吧!”
这使它看起来似乎是在宏应用之前对参数进行评估。
发布于 2021-09-28 04:41:02
首先,您应该试着看看宏扩展是什么:
scheme@(guile-user)> (use-modules (language tree-il))
scheme@(guile-user)> (tree-il->scheme (macroexpand '(foo bar)))缩进编辑以提高清晰度
(if ((@@ (#{ g184}#) eqv?) bar (quote bar))
(begin ((@@ (#{ g184}#) write) "Caller said BAR!")
((@@ (#{ g184}#) newline))))因此,(foo bar)被转换为(在移除@@s之后):
(if (eqv? bar (quote bar))
(begin (write "Caller said BAR!")
(newline)))在进行评估之前。现在这个错误是有意义的,不是吗?
现在,如果引用word参数会发生什么?看看它是如何扩展的:
(define-syntax foo
(syntax-rules ()
((foo word)
(if (eqv? 'word 'bar) (begin (write "Caller said BAR!") (newline))))))https://stackoverflow.com/questions/69355751
复制相似问题