Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Question on Condition (/;)

Condition has attribute HoldAll which prevents evaluation of its first argument before applying the Condition. But for some reason Condition evaluates its first argument even if the test gives False:

In[1]:= Condition[Print[x],False]
During evaluation of In[1]:= x
Out[1]= Null/;False

Why is this? For what purposes Condition evaluates its first argument if the test gives False? In which cases this behavior can be useful?

P.S. Its behavior differs when the Condition is used as the second argument of SetDelayed:

In[5]:= f:=Condition[Print[x],False]; f
Out[6]= f

This is what I expected for the all cases.

like image 367
Alexey Popkov Avatar asked May 03 '11 06:05

Alexey Popkov


People also ask

What are condition questions?

Conditional questions are those that are not automatically asked: you will have to answer them only depending on your previous answers.

Can Google Forms do conditional questions?

Yes, Google Forms has a conditional questioning feature that will allow you to show questions based on the user's response.

How do I create a condition in Google Forms?

First, select the question. Then click the Add section button on the menu to the right. After you've added the section, click the three dots in the bottom right to add logic to the question by selecting Go to section based on answer. There are now dropdowns to the right of each answer option.


1 Answers

Condition use often depends on what is in the left hand side, so it must evaluate the LHS at least to some degree. Consider:

MatchQ[3, a_ /; IntegerQ[a]]
   True
p = {a_, b_};

MatchQ[{3, 0.2}, p /; IntegerQ[a] && b < 1]
   True

Both for this and from this, I would have guessed that Condition had attribute HoldRest rather than HoldAll. It probably needs HoldAll for some internal use, perhaps related to the SetDelayed usage.

like image 177
Mr.Wizard Avatar answered Nov 26 '22 08:11

Mr.Wizard