开发者

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

开发者 https://www.devze.com 2023-01-20 04:51 出处:网络
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))

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

精彩评论

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