在Common Lisp中编写++宏 [英] Writing a ++ macro in Common Lisp

查看:50
本文介绍了在Common Lisp中编写++宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于语义上的原因,我一直在尝试编写一个Lisp宏,该宏将替代其他编程语言中的++.我尝试了几种不同的方法来执行此操作,但是它们似乎都不起作用,并且都被解释器接受,因此我不知道我是否具有正确的语法.我对这个定义的想法是

I've been attempting to write a Lisp macro that would perfom the equivalent of ++ in other programming languages for semantic reasons. I've attempted to do this in several different ways, but none of them seem to work, and all are accepted by the interpreter, so I don't know if I have the correct syntax or not. My idea of how this would be defined would be

(defmacro ++ (variable)
  (incf variable))

但这在尝试使用它时给了我SIMPLE-TYPE-ERROR.什么会使它起作用?

but this gives me a SIMPLE-TYPE-ERROR when trying to use it. What would make it work?

推荐答案

请记住,宏返回要求值的表达式.为此,您必须反引号:

Remember that a macro returns an expression to be evaluated. In order to do this, you have to backquote:

(defmacro ++ (variable)
   `(incf ,variable))

这篇关于在Common Lisp中编写++宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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