Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Check if all values in a list are T

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
Anonymous
2435 Views, 6 Replies

Check if all values in a list are T

I am having a bad Monday brain. I have 2 lists of lists I need to check if each value in the first list has matching values in the second list. I create a temporary list that has a T or nil depending on outcome - example - Matching list (T T T) Non-matching list (T nil nil) How can I check if all items in temporary list are T, else nil?
6 REPLIES 6
Message 2 of 7
marko_ribar
in reply to: Anonymous

Command: (vl-every '(lambda ( x ) (eq x T)) '(T T T))
T

Command: (vl-every '(lambda ( x ) (eq x T)) '(T T nil))
nil

 HTH

Marko Ribar, d.i.a. (graduated engineer of architecture)
Message 3 of 7
Kent1Cooper
in reply to: Anonymous

This will also work with lists of explicitly T-or-nil-only values, BUT if checking merely that they are all not nil will do as well as checking whether they're all T, there can be a big advantage to doing it this way:

 

(apply 'and YourList)

 

You might be able to shortcut the whole operation [depending on how you're making your temporary lists, you might be able to eliminate that step entirely], because you don't need to convert non-nil values explicitly to T's.  For example, if you have some variables of assorted kinds of values:

 

(setq a 12 b "yes" c pi)

 

they're not all T, but they are all non-nil, so:

 

Command: (apply 'and (list a b c))
T

 

whereas checking whether they're all explicitly T will return nil.

 

If something has resulted in any of them being nil:

 

(setq a 12 b "yes" c nil)

 

Command: (apply 'and (list a b c))
nil

Kent Cooper, AIA
Message 4 of 7
Lee_Mac
in reply to: Anonymous

mracad wrote:
I have 2 lists of lists. I need to check if each value in the first list has matching values in the second list.

 

It sounds like the temporary list is unnecessary - could you possibly give an example of the format of your data, and the test that you need to perform as we may be able to improve the efficiency of the process.

 

Lee

Message 5 of 7
Kent1Cooper
in reply to: Anonymous


@Anonymous wrote:
.... I have 2 lists of lists I need to check if each value in the first list has matching values in the second list. I create a temporary list that has a T or nil depending on outcome - example - Matching list (T T T) Non-matching list (T nil nil) How can I check if all items in temporary list are T, else nil?

My first reply was to the Subject line and the last sentence of your original message.  But given Lee's reminder that the beginning of it is a little different....

 

If the 2 lists are in T-or-nil values only, you can check whether they have the same pattern of T's and nil's [matching values in the same positions], if that's the kind of comparison you want to make, with:

 

(not (member nil (mapcar '= list1 list2)))

 

which will return T if they do align/agree in that way, and nil if they don't.

 

Command: (not (member nil (mapcar '= '(T T T) '(T T T))))
T

 

Command: (not (member nil (mapcar '= '(T nil T) '(T nil T))))
T

 

Even if all the values are nil, if that's the case in both lists, they agree, and it will return T:

Command: (not (member nil (mapcar '= '(nil nil nil) '(nil nil nil))))
T

 

But if there's any difference, even if only in the order of the same collection of values:

Command: (not (member nil (mapcar '= '(T T nil) '(T nil T))))
nil

 

However, if you're looking only for having matching values, but not necessarily in the same positions, that's another story [but doable].

Kent Cooper, AIA
Message 6 of 7
hmsilva
in reply to: Anonymous


@Anonymous wrote:
I am having a bad Monday brain. I have 2 lists of lists I need to check if each value in the first list has matching values in the second list. I create a temporary list that has a T or nil depending on outcome - example - Matching list (T T T) Non-matching list (T nil nil) How can I check if all items in temporary list are T, else nil?

If the goal is test each list element position and value, why not use the equal function

 

(equal lista listb)

 

Henrique

 

EESignature

Message 7 of 7
Anonymous
in reply to: Anonymous

Thanks for the suggestions. I picked (apply 'and as the simplest and fastest for my needs.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost