【问题标题】:Common Lisp: This is not a number NIL and &rest parametersCommon Lisp:这不是数字 NIL 和 &rest 参数
【发布时间】:2014-06-11 23:21:09
【问题描述】:

在下面代码中的这一行((pointerp (first args)) (mem-aref (%vector-float-to-c-array (first args)) :float (second args))) 上,(second args) 编译时带有警告This is not a number NIL。该功能有效,但我如何仅使用 Lisp 以独立于实现的方式摆脱此警告。解决方案需要非常快。该代码需要很长时间才能正确运行并且运行良好,因此我无法真正更改它的操作。您不认识的功能与警告消失无关紧要...提前感谢您的帮助。

(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)))

【问题讨论】:

    标签: common-lisp cffi


    【解决方案1】:

    如果(second args)NIL,那么args 没有second 或者它的secondNIL。但是,mem-aref 的第三个参数必须是一个数字,因为它是一个索引。问题就在这里。

    如果在您的程序中(second args) 被允许为NIL(或不存在),那么您必须测试这种可能性并避免将NIL 传递给mem-aref(也许通过省略它可选参数)。如果(second args)不允许成为NIL,那么这个bug就在你程序的其他地方。

    例如(未经测试),

    (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)))
    

    【讨论】:

    • (second args) 可以为零...你能帮我解决一个问题,以消除警告...谢谢你加入 btw :)
    • 可能和上面的差不多。
    • 太棒了,这就像一种魅力,是跳出框框思考的好方法
    猜你喜欢
    • 2015-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-17
    • 2019-08-03
    相关资源
    最近更新 更多