How do I know if a list contains NIL?

I have the following list:

(XOXOX NIL X NIL O) 

If I do (find 'x (XOXOX NIL X NIL O)) , it returns X , so I know that the list contains X But if I do (find nil (XOXOX NIL X NIL O)) , I get NIL , which is pointless because I would get NIL even if my list did not contain NIL .

How do I know if my list contains NIL or not?

+4
source share
4 answers

You can use position

 (position NIL '(XOXOX NIL X NIL O)) --> 5 
+3
source

It may not be as efficient with the processor as others, but I would most likely write it as I speak English:

 (some #'null '(XOXOX NIL X NIL O)) 
+3
source

Something like this work?

 (or (map (lamdba (x) (eq? x nil)) '(XOXOX NIL X NIL O))) 

I donโ€™t have a CL right now, but I think it will work. (or works like that in CL, right? or is there any function?

EDIT:

Wait a second, zero is considered false, right?

 (apply and '(XOX NIL)) ;Or (all '(XOX NIL)) ;Whichever works in CL. 
0
source

NOT is what you are looking for.

Here is an example:

  (reduce #'(lambda (ab) (or ab)) (mapcar 'not '(1 2 3 4))) 
-1
source

Source: https://habr.com/ru/post/1340090/


All Articles