# Sequence and boolean And

I'm trying to figure out if a calculation result it's a valid result. As it return a matrix, I need to test that every element it's a number, so I thought this could work...

numericTable = Range ~Partition ~ 4;
check=( NumberQ/@Flatten@numericTable) /.List->Sequence ;
And[check]


This should return True or False depending if exist or not a non-numerical element in the matrix. But it returns

Sequence[True,True,True,True,True,True,True,True,True,True,True,True]


So I don't understand anything, because...

In[]:= Sequence[True,True,True,True,True,True,True,True,True,True,True,True]===check
And[Sequence[True,True,True,True,True,True,True,True,True,True,True,True]]
Out[]= True
Out[]= True


Where's the catch ???

• Why not just And @@ (NumberQ /@ Flatten@numericTable)? – Pinguin Dirk Feb 21 '13 at 16:41
• Or a combination of MatrixQ and NumberQ as in the docs ? – b.gates.you.know.what Feb 21 '13 at 16:42
• @PinguinDirk you're right... in fact I found some code where I did that, but still I would like to understand where's the problem with Sequence... – crazzymath Feb 21 '13 at 16:44
• @belisarius - thanks for the example - I will post an answer - feel free to edit that later. thanks – Pinguin Dirk Feb 21 '13 at 17:02
• Duplicate: mathematica.stackexchange.com/q/16694/5 – rm -rf Feb 21 '13 at 17:09

As in the comments above, we see that the HoldAll attribute is causing the "problem".

Note that if we unset HoldAll it works:

Unprotect@And;
ClearAttributes[And, HoldAll];
Protect@And;


Then:

And[check]


True

EDIT: based on Oleksandr R.'s comment, I must stress that I showed this only to illustrate the "problem". It is not a good idea to unset HoldAll for And, as noted below in his comment. So as in my comment to the question above, you can just use Evaluate to get the result:

And[Evaluate@check]


to get to the same result.

(END EDIT)

You could directly apply And on what you constructed:

And @@ (NumberQ /@ Flatten@numericTable)


but better check MrWizard's answer for a direct solution of your problem.

• Well done! :) .. – Dr. belisarius Feb 21 '13 at 17:31
• & thanks for your input! – Pinguin Dirk Feb 21 '13 at 17:45
• Removing HoldAll is a very bad idea in the case of And: it destroys the short-cut evaluation semantics, which are frequently relied upon for correctness. If you know you don't need this in a particular situation, a better way is to apply Evaluate for that case only or inject the evaluated argument using With. I'm downvoting for now due to this, but will remove it when you update your answer to override HoldAll more "correctly". – Oleksandr R. Feb 21 '13 at 21:06
• @OleksandrR.: it was meant to illustrate the point, but thanks for pointing it out that it is misleading - I will edit my post accordingly. Thanks for the input! – Pinguin Dirk Feb 21 '13 at 21:23
• Earned the +1 after your edit – Rojo Feb 21 '13 at 21:29

In addition to HoldAll as highlighted by Pinguin Dirk the other component of the behavior is that And directly returns single arguments:

And

73


Combined, And[check] spits out check which at the top level evaluates to Sequence[True, . . .].

One problem with your method for checking the matrix is that it does not short-circuit on a non-numeric value, and therefore it may waste time. That is, NumberQ is applied to every element even if the very first one is non-numeric. A faster approach is to use the built-in MatrixQ:

MatrixQ[numericTable, NumberQ]

True

• I accept the @PinguinDirk response because I only accept just one and he was first, but thank you for your response (and other responses all over the MSE, I'm a fan ;-) – crazzymath Feb 25 '13 at 17:47