Chicken方案中宏扩展期间的错误 [英] Error during expansion of macro in Chicken Scheme
问题描述
我正在学习Scheme中的宏系统如何工作,并且试图使我的代码看起来更像JavaScript.所以我以为我将从function
宏开始.这就是我希望函数定义看起来的样子:
I'm learning how the macro system in Scheme works and I'm trying to make my code look more JavaScript-y. So I thought I would start with the function
macro. This is how I want a function definition to look:
(function id (x) x)
它应该扩展为以下内容:
It should expand to the following:
(define (id x) x)
所以我写一个如下的宏:
So I write a macro as follows:
(define-syntax function
(lambda (name args . body)
`(define (,name ,@args) ,@body)))
但是,当我使用它时,出现以下错误(在Chicken Scheme中):
However when I use it I get the following error (in Chicken Scheme):
Error: during expansion of (define ...) - in `define' - lambda-list expected: (define ((function id (x) x) . #<procedure (rename sym1348)>) #<procedure (compare s11400 s21401)>)
Call history:
<syntax> (function id (x) x)
<eval> (##sys#cons (##core#quote define) (##sys#cons (##sys#cons name args) body))
<eval> (##sys#cons (##sys#cons name args) body)
<eval> (##sys#cons name args) <--
我要去哪里错了?另外,我如何阅读此类错误消息以能够自己调试程序?
Where am I going wrong? In addition how do I read such error messages to be able to debug the program myself?
推荐答案
在Scheme中,使用语法rules():
In Scheme, using syntax-rules():
(define-syntax function
(syntax-rules ()
((function name (args ...) body ...)
(define (name args ...) body ...))))
您看到的错误是,Chicken Scheme的编译器显然希望define-syntax
的第二种形式是宏扩展过程-它们通常需要用于重命名和比较标识符的参数.宏中的lambda
不会产生合适的功能-syntax-rules
会产生合适的功能.
The error you are seeing is that apparently Chicken Scheme's compiler expects the second form of define-syntax
to be a macro expansion procedure - they typically require arguments for renaming and comparing identifiers. The lambda
in your macro doesn't produce a suitable function - syntax-rules
does.
以上内容保证卫生.
这篇关于Chicken方案中宏扩展期间的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!