Common Lisp: This is not a number NIL and &rest parameters

女生的网名这么多〃 提交于 2019-12-13 10:22:55

问题


On this line ((pointerp (first args)) (mem-aref (%vector-float-to-c-array (first args)) :float (second args))) in the below code the (second args) compiles with the warning This is not a number NIL. The function works but how do I get rid of this warning in an implimentation independent way using just Lisp. The solution would need to be really fast. The code took a long time to get to right and runs great so I can't really change the operation of it. the functions you don't recognize don't really matter as far getting warning to go away...Thank you in advance for any help.

(defun vector-float (&rest args)
  (cond ((eq (first args) nil) (return-from vector-float (%vector-float)))
    ((listp (first args))
     (c-arr-to-vector-float (first args)))
    ((symbolp (cadr args)) (%vector-float-size (first args)))
    ((pointerp (first args)) (mem-aref (%vector-float-to-c-array (first args)) :float (second args)))
    (t nil)))

回答1:


If (second args) is NIL then either args has no second or its second is NIL. However, the third argument to mem-aref must be a number since it is an index. Therein lies the problem.

If in your program (second args) is allowed to be NIL (or to not exist), then you'll have to test for that possibility and avoid passing NIL to mem-aref (maybe by leaving out that optional argument). If (second args) is not allowed to be NIL, then the bug is somewhere else in your program.

E.g. (untested),

(defun vector-float (&rest args)
    (cond
        ((null (first args))
            (return-from vector-float (%vector-float)))
        ((listp (first args))
            (c-arr-to-vector-float (first args)))
        ((symbolp (second args))
            (%vector-float-size (first args)))
        ((pointerp (first args))
            (if (null (second args))
                (mem-aref (%vector-float-to-c-array (first args)) :float)
                (mem-aref (%vector-float-to-c-array (first args)) :float (second args))))
        (t nil)))


来源:https://stackoverflow.com/questions/23306757/common-lisp-this-is-not-a-number-nil-and-rest-parameters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!