开发者

"is not of type LIST" error

开发者 https://www.devze.com 2023-04-03 20:41 出处:网络
I am doing Exercise 14.11 in \"A Gentle Introduction to Symbolic Computation,\" and wrote the following function:

I am doing Exercise 14.11 in "A Gentle Introduction to Symbolic Computation," and wrote the following function:

(defmacro compile-machine (nodes)
  `(progn ,@(mapcar #'compile-node nodes)))

When calling (compile-machine *nodes*), where *nodes* is a list of NODE structures, I get the following error:

Error: *NODES* is not of type LIST.

So I see what *nodes* is, and

CL-USER 116 > *nodes*
(#<Node START> #<Node HAVE-5> #<Node HAVE-10> #<Node HAVE-15> #<Node HAVE-20> #<Node HAVE-25> #<Node END>)

CL-USER 117 > (type-of *nodes*)
CONS

CL-USER 118 > (listp *nodes*)
T

It seems as if *nodes* is indeed a list. What am I doing wrong here?

EDIT: More code that can perhaps clarify

(defun compile-arc (arc)
  `((equal this-input ',(arc-label arc))
    (format t "~&~A" ,(arc-action arc))
    (,(node-name (arc-to arc)) (rest input-syms))))

(defun compile-node (node)
  `(defun ,(node-name node) (input-syms &aux (this-input (first input-syms)))
     (cond (开发者_JAVA百科(null input-syms) ',(node-name node))
           ,@(mapcar #'compile-arc (node-outputs node)) ;isn't this basically the same?
           (t (error "No arc from ~A with label ~A."    ;and yet Lisp doesn't complain
                     ',(node-name node) this-input)))))


You are not writing a function but a macro.

A macro operates on code. Your code (compile-machine *nodes*) gets macro-expanded to other code, which then is executed. The argument to your macro is the unevaluated symbol *nodes*. You cannot mapcar a symbol.

It seems to me that you actually do want to write a function. Use defun for that:

(defun compile-machine (nodes)
  (mapcar #'compile-node nodes))


The value of *NODES* is a list, but itself it is a symbol.


nodes is not a list in this case, it's a symbol. The "magic" of macros is that you, the macro writer, get to evaluate the argumetns -- not the Lisp reader.

So, you're passing in nodes, which is a symbol. You'll need to deference it to get it's value.

Perhaps like this (caveat I'm really rusty here on this, this may well be wrong, but the basic premise is close)

(defmacro compile-machine (nodes)
  `(progn ,@(mapcar #'compile-node ,nodes)))
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号