Seems like maybe Mathematica 9 didn’t have AllTrue[] function.
Anyway, I’m working with Pascal’s triangle, trying to see if all members [except first/last] are divisible by the row number. I create a list or members, a list of divisibilities [evaluates to true/false].
But then I want to check the list of disvisibilities for ALL being True, which is what I’m really looking for.
So, I want to evaluate a variable that has a list of boolean true/false values and evaluate if all members are “true” [I suppose I could also evaluate if ‘any’ are False, but I’d prefer “all true”].
So, how would I go about that without the AllTrue[] function, which I’m guessing was added in a later revision?
Seems like this should be a fairly trivial thing? But not sure how to go about it?
I mean, I guess I could go the opposite route and say something like:
As you can see, I simply took the row divisibility-by-row-number list ad did a MemberQ check versus “False,” and then Simply did a check whether the check is false or not, which returns true if false, or false if true, to “invert” the value.
Ridiculous workaround. But, as long as it works, right?
Seriously though, is there an easier way to check that all list members are specifically true as opposed to “no members are false”?
I feel like there should be, save for the fact AllTrue[] apparently wasn’t a thing in Mathematica 9 (I know, I’m a few versions back; been meaning to upgrade for a while, just haven’t gotten around to it yet).
[My kludge-y solution got it done in basically 2 lines with MemberQ[list, False] and then simply doing a check whether the output is “[Output]==False” to flip the output bit from False to True. ]
Careful. For conjunctions, Mathematica performs a short circuit evaluation, i.e. it stops anding up Booleans as soon as it encounters false. Btw this is standard procedure for any modern programming languages.
As I see it, the OP had a list consisting of only True or False and wanted to see if all elements were True. The additional condition of OddQ somehow crept in later in the discussion.
It performs exactly as I would expect. In some cases the test you wish to run is very fast, so that it is faster to run that test on all the inputs and check the results after than it is to do the incremental checks needed for the short circuit.
As mentioned above, OddQ is written in low-level kernel code and is optimised when mapped: