Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
在 Racket 中,你可以这样定义语法:
(define-syntax foo (syntax-rules () ((_ "abc") 'xyz)))
通过运行
(foo "abc")
它返回'xyz。使用定义实现这个:
(define (foo x) (match x ["abc" 'xyz]))
这次,
仍然返回'xyz。使用这些不同的形式有什么区别?
define-syntax定义一个宏。宏在编译时处理,并产生要编译的新代码,函数在运行时执行。因此,宏不能访问运行时变量,也不能评估它们的参数。要查看差异,请尝试:
define-syntax
(define param "abc") (foo param)
用你定义的两种方式foo。这将适用于函数,但不适用于宏。
foo
尝试这个:
(define abc "abc") (foo abc)
您将看到两个版本之间非常不同的结果。