开发者

Checking whether every list in a list is null in Common Lisp

I know that I can check whether a list of lists only contains null lists like this

CL-USER> (null (find-if (lambda (item) (not (null item))) my-list))

where my-list is a list of lists.

For example:

CL-USER> (null (find-if (lambda (item) (not (null item))) '(nil (bob) nil)))
NIL
CL-USER> (null (find-if (lambda (item) (not (null item))) '(() () ())))
T

But isn't there a shorter, easier way of doing this in Lisp? If so, how?开发者_开发知识库


The higher order function every takes a predicate function and a list and returns true iff the predicate returns true for every element in the list.

So you can just do:

(every #'null my-list)


(find-if #'identity list)

(not (find-if-not #'null list))

Consult the Common Lisp HyperSpec for the full list of functions for lists and sequences.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜