常见的lisp错误:该值不是预期的类型编号

时间:2013-04-26 07:03:49

标签: common-lisp

我是lisp的新手,我的功能有些麻烦:

(setf (symbol-function 'reduce-our)
    #'(lambda(new-expression)
            (setf expression nil)
              (loop while (not (equal new-expression expression)) do
                        (setf expression new-expression)
                        (setf new-expression (reduce-once-our expression))
                        (if (not (equal 'new-expression 'expression))
                            (format t " ==> ~A Further reductions are impossible.~%"
                             new-expression)
            new-expression))))

(reduce-our '(^ x => x))

这会产生下一个错误:

Error: The value ^ is not of the expected type NUMBER.

我认为lisp正试图在while循环中评估我的输入列表,但是

(not (equal nil '(^ x => x)))

工作得很好,我确信我的功能会做同样的检查。所以。我不明白这个错误发生在哪里以及为什么会发生。

1 个答案:

答案 0 :(得分:3)

您确定此功能发生错误吗?你应该看一下回溯。

此外:

(setf (symbol-function 'reduce-our)
   #'(lambda (new-expression)
      ...))

通常写为

(defun reduce-our (new-expression)
   ...)

然后:

(setf (symbol-function 'reduce-our)
  #'(lambda(new-expression)
      (setf expression nil) ...

变量expression在哪里引入?这是未宣布的。设置值不会声明变量。

然后:

while (not (foo ...))

只是

until (foo ...)

(if (not (foo)) a b)

(if (foo) b a)

另外:改进缩进。 Lisp的编辑会为你做这件事。它增加了你和他人的可读性。

相关问题