Chicken方案中宏扩展期间的错误 [英] Error during expansion of macro in Chicken Scheme

查看:98
本文介绍了Chicken方案中宏扩展期间的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆