我的问题是:
假设有一个列表 (a b c d)
在继续我的代码之前,我想确保其中有 4 个元素。
如果列表显示 (a b c)
我希望它返回 nil,因为这不是我想要的。
谁能帮忙=)?
最佳答案
使用 LENGTH 函数:
(when (>= (length list) 4)
;; rest of your code
)
如果你需要一个不扫描整个列表只是为了查看是否有N个元素的方法,你可以使用这个函数:
(defun length-at-least (list n)
(cond ((zerop n) t)
(list (length-at-least (cdr list) (1- n)))
(t nil)))
然后你可以这样做:
(when (length-at-least list 4)
;; rest of your code
)
关于list - 检查列表中是否有一定数量的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13169219/