
Re: Do we need a When function?
Posted:
Mar 17, 2014 2:27 AM


On 15/03/2014 07:51, Itai Seggev wrote:
> > If[NumericQ[x], f[x]] (*are NumberQ, if want an actual number type*) >
The difference, which at least Szabolcs appreciates, can be seen in this example:
In[3]:= expr = If[NumericQ[x], ToString[x]]
In[4]:= expr /. x > 2.1
(* The output here is Null *)
In[7]:= SetAttributes[When, HoldAll]; When[cond_, val_] := val /; cond
In[9]:= expr = When[NumericQ[x], ToString[x]]
Out[9]= When[NumericQ[x], ToString[x]]
In[10]:= expr /. x > 2.1
Out[10]= "2.1"
The effect of When is to delay its evaluation until something is true  which can be quite useful!
David Bailey http://www.dbaileyconsultancy.co.uk

