MathGroup Archive 2014

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Do we need a When function?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg132440] Re: Do we need a When function?
  • From: David Bailey <dave at removedbailey.co.uk>
  • Date: Mon, 17 Mar 2014 02:26:51 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • Delivered-to: l-mathgroup@wolfram.com
  • Delivered-to: mathgroup-outx@smc.vnet.net
  • Delivered-to: mathgroup-newsendx@smc.vnet.net
  • References: <20140308074231.0A4506A0B@smc.vnet.net> <lg10pf$50i$1@smc.vnet.net>

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







  • Prev by Date: Re: NIntegrate and double integral -- very slow
  • Next by Date: Re: What's in an expression?
  • Previous by thread: Re: Do we need a When function?
  • Next by thread: Re: Do we need a When function?