我写了个假象?宏如下
(defmacro if-pred?
([pred lst then] `(if (~pred ~lst) ~then nil))
([pred lst then else] `(if (~pred ~lst) ~then ~else)))现在我想建造一个空的?从那来的宏。
(defmacro if-empty? [lst then & else]
(if-pred? empty? lst then else))我想用如果-空的话?像这样:
(if-empty? '()
(println "empty")
(println "not-empty"))但很明显如果-空的话?不起作用。当我运行上面的代码时,不-空的会被打印出来,而不是传递给if-空的列表是否重要?不管是不是空的。打印后,将引发不为空的 java.lang.NullPointerException。
发布于 2011-06-06 13:44:37
您没有引用(如果-空的话)的展开,而是将可选的of参数强制放到列表中。
(defmacro if-empty?
([lst then else]
`(if-pred? empty? ~lst ~then ~else))
([lst then]
`(if-pred? empty? ~lst ~then)))发布于 2011-06-06 13:41:29
宏需要返回代码。试试这个:
(defmacro if-empty? [lst then & else]
`(if-pred? empty? ~lst ~then ~@else))发布于 2011-06-06 13:43:59
是你的-空的?宏。你忘了倒扣身体。应:
(defmacro if-empty? [lst then & else]
`(if-pred? empty? ~lst ~then ~else))https://stackoverflow.com/questions/6252636
复制相似问题