计算列表中有多少个正数(NIL 不是数字?

  • 本文关键字:NIL 数字 列表 多少 计算 lisp
  • 更新时间 :
  • 英文 :


我有一个列表:(setq listy '(4 -3 8 99 -40 61 12 -8 2 -20))

我的函数lenPos应该找到列表中所有正数的长度(即 6)。但是,我收到此错误:

*** - +: NIL is not a number

在对 if 语句进行任何数字检查之前,我正在检查我的列表是否为 null。所以我不明白错误来自哪里。

;num of positive elems
(defun lenPos (list)
    (cond
        ((null list) 0) ;if null list return 0
        (t (cond ;else
            ((> (car list) 0) (+ 1 (lenPos (cdr list))))
        ))
    )
)

如果list不是null,并且第一个元素不是正数,则该函数不会显式返回任何内容,因此隐式返回nil

最新更新