Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.math.mathematica

Topic: Do we need a When function?
Replies: 9   Last Post: Mar 23, 2014 5:01 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
David Bailey

Posts: 714
Registered: 11/7/08
Re: Do we need a When function?
Posted: Mar 23, 2014 5:01 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

On 19/03/2014 08:23, Itai Seggev wrote:
> David,
>
> Firstly, I would appreciate it if you didn't quote these tiny fragments of my
> replies, which suggest I didn't already understand/address points you raise
> (or that perhaps you didn't bother to read my email in its entirety).
>

>> 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!

>
> But on the other hand you have these behaviors:
>
> In[59]:= ClearAll[When]
> SetAttributes[When, HoldAllComplete];
> When[cond_, val_] := val /; cond
>
> In[62]:= expr = When[NumericQ[x], ToString[x]]
>
> Out[62]= When[NumericQ[x], ToString[x]]
>
> In[64]:= Block[{x = 2.1}, expr ]
>
> Out[64]= When[NumericQ[x], ToString[x]]
>
> In[65]:= x = 2.1; expr
>
> Out[65]= When[NumericQ[x], ToString[x]]
>
> Are these useful?
>
>
>

They are not useful - but I didn't define When with Attribute
HoldAllComplete, but with HoldAll - so I don't see your point!

David Bailey
http://www.dbaileyconsultancy.co.uk




Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.