lisp错误处理 - 检查变量是否为nil

时间:2016-10-24 10:01:12

标签: loops for-loop foreach common-lisp

我最近一直在处理一些lisp,我正在编写一个返回元素组合的函数。它工作得很好,但它仍然会给我一个警告错误>参数Y不是NUMBER:NIL

我的代码:

(defun printo (x y z)
  (if (and x y z)      
      (format t "~a and ~a  difference: ~a ~%" x y z)
      ))

(defun getDiff (x y)
  (return-from getDiff (abs (- x y))))


(defun distcalc (lista)
  (loop for k from 0 to (list-length lista)
     do (loop for j from 0 to (list-length lista)
       do (let ((pivot (nth k lista))(base (nth j lista)))
        (if (nth j lista)(printo  pivot  base (abs (- base pivot))
                      ))
        ))
       ))

(distcalc '(1 10 20 25 13))

由于我是初学者,我想我可能错过了某处的错误处理,但是粘液把我扔在错误屏幕上真的很烦人!

感谢您的帮助。

2 个答案:

答案 0 :(得分:7)

请使用标准格式和命名:

(defun printo (x y z)
  (if (and x y z)
      (format t "~a and ~a  difference: ~a ~%" x y z)))

(defun get-diff (x y)
  (return-from get-diff (abs (- x y))))

(defun distcalc (lista)
  (loop for k from 0 to (list-length lista)
        do (loop for j from 0 to (list-length lista)
                 do (let ((pivot (nth k lista))
                          (base (nth j lista)))
                      (if (nth j lista)
                          (printo pivot base (abs (- base pivot))))))))

(distcalc '(1 10 20 25 13))

如果您不需要if中的替代方案,请使用when

(defun printo (x y z)
  (when (and x y z)
    (format t "~a and ~a  difference: ~a ~%" x y z)))

(defun get-diff (x y)
  (return-from get-diff (abs (- x y))))

(defun distcalc (lista)
  (loop for k from 0 to (list-length lista)
        do (loop for j from 0 to (list-length lista)
                 do (let ((pivot (nth k lista))
                          (base (nth j lista)))
                      (when (nth j lista)
                        (printo pivot base (abs (- base pivot))))))))

(distcalc '(1 10 20 25 13))

您不需要returnreturn-from;函数体返回 最后一个表格的值。我想您想使用get-diff

(defun printo (x y z)
  (when (and x y z)
    (format t "~a and ~a  difference: ~a ~%" x y z)))

(defun get-diff (x y)
  (abs (- x y)))

(defun distcalc (lista)
  (loop for k from 0 to (list-length lista)
        do (loop for j from 0 to (list-length lista)
                 do (let ((pivot (nth k lista))
                          (base (nth j lista)))
                      (when (nth j lista)
                        (printo pivot base (get-diff base pivot)))))))

(distcalc '(1 10 20 25 13))

错误是循环to包括结尾;你想要below

(defun printo (x y z)
  (when (and x y z)
    (format t "~a and ~a  difference: ~a ~%" x y z)))

(defun get-diff (x y)
  (abs (- x y)))

(defun distcalc (lista)
  (loop for k from 0 below (list-length lista)
        do (loop for j from 0 below (list-length lista)
                 do (let ((pivot (nth k lista))
                          (base (nth j lista)))
                      (when (nth j lista)
                        (printo pivot base (get-diff base pivot)))))))

(distcalc '(1 10 20 25 13))

但是,您根本不需要索引,因此您可以简单地遍历列表:

(defun printo (x y z)
  (when (and x y z)
    (format t "~a and ~a  difference: ~a ~%" x y z)))

(defun get-diff (x y)
  (abs (- x y)))

(defun distcalc (lista)
  (loop for pivot in lista
        do (loop for base in lista
                 when base
                 do (printo pivot base (get-diff base pivot)))))

(distcalc '(1 10 20 25 13))

我们现在可以更好地看到nil中的任何lista也是一个 外循环中的问题:

(defun printo (x y z)
  (when (and x y z)
    (format t "~a and ~a  difference: ~a ~%" x y z)))

(defun get-diff (x y)
  (abs (- x y)))

(defun distcalc (lista)
  (loop for pivot in lista
        when pivot
        do (loop for base in lista
                 when base
                 do (printo pivot base (get-diff base pivot)))))

(distcalc '(1 10 20 25 13))

答案 1 :(得分:3)

您的错误是(loop for k from 0 to (list-length lista))会为您k 0-5而不是0-4(nth 5 '(1 10 20 25 13))会给您nil(- 1 nil)同样的错误。每次做nth都不好。或许应该这样做:

(defun distcalc (lista)
  (loop :for pivot :in lista
        :do (loop :for base :in lista
                  :if base
                  :do (printo pivot base (abs (- base pivot))))))