Is it possible to expand a macro into several elem

2019-08-06 19:27发布

I want a macro my-macro which can expand to 1 2 3 rather than (1 2 3), so that

(list (my-macro) 4 5) -> (1 2 3 4 5)

Is this possible?

2条回答
smile是对你的礼貌
2楼-- · 2019-08-06 20:09

No.

Neither a macro nor a read macro can do that in Common Lisp.

The only solution if you really need this is to write a full reader yourself where you don't use read at all (the problem is that read will recursively call itself, and not your version).

A full compliant reader is quite a complex thing to do, but it can be simple if you only need a subset of the features and you don't need to use it for example to read Common Lisp code written by others.

查看更多
贼婆χ
3楼-- · 2019-08-06 20:12

No, macros cannot expand to more than one value. The typical thing to do when you need a macro to expand to multiple pieces of code is to wrap the return value in a progn.

In your example in the comments, it looks as if you are using macros not as syntactic abstractions, but as cheap-and-cheerful function optimizations, the usual response to that is "please don't do it, it is wrong and doesn't actually do what you want".

查看更多
登录 后发表回答