MathGroup Archive 2014

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

Search the Archive

Re: Do we need a When function?

  • To: mathgroup at
  • Subject: [mg132433] Re: Do we need a When function?
  • From: Itai Seggev <itais at>
  • Date: Sat, 15 Mar 2014 03:45:25 -0400 (EDT)
  • Delivered-to:
  • Delivered-to:
  • Delivered-to:
  • Delivered-to:
  • References: <>

On Thu, Mar 13, 2014 at 03:10:50AM -0400, David Bailey wrote:
> On 10/03/2014 08:37, Itai Seggev wrote:> On Sat, Mar 08, 2014 at 02:42:31AM
>  >
>  > I'm not sure what your use case is.  F[x] will of course stay 
> unevluated until
>  > x has some value which F knows what to do with...
>  >
>  > --
>  > Itai Seggev
>  > Mathematica Algorithms R&D
>  > 217-398-0700
>  >
> A typical use would be an expression like When[NumberQ[x],ToString[x]]

That isn't very illuminating to me. More below...

> Somehow everyone seems to be missing the point here - of course I can 
> solve the problem by writing some code - indeed I gave a solution in my 
> original post - but lots of Mathematica functions can be replaced by 
> other expressions using more primitive functions, but such functions are 
> provided for convenience and expressiveness.
> Supplying an expression that only evaluates when some condition is true, 
> seems a pretty useful feature.
> The difference between If and When, is obvious:
> If[NumberQ[x],f[x]]
> Null
> When[NumberQ[x],f[x]]
> When[NumberQ[x],f[x]]
> When delays evaluation until is condition is True.

Of course.  But in fairness, the source of the confusion for me (and perhaps
others) is that you confused the issues in your original email.  
> Obviously, one way to do that is to write something like:
>   If[x<1000000000000,f[x]]
> This will stay unevaluated until x is assigned to a number - as required
> - but it is very ugly because it makes it hard to understand what is
> going on.

Why isn't this simply

If[NumericQ[x], f[x]]   (*are NumberQ, if want an actual number type*)

But I'm further confused by your statement that it stays unevlauate until x is
assigned a number.  That's not true, it will evaluate to Null unless x is
numeric prior to the start of evaluation.
You'll notice you claimed you could acheive what you wanted with If, and then I
point out the difference between If and When. 

> If you want to hold an expression until (and unless) some condition is 
> satisfied, When would seem to me to be the most expressive way to write 
> this.

I still don't understand what your use case is.  I agree that When has the
properties stated in your most recent email.  But these expressions won't
magically transform themselves.  You'll have to reevaluate them from scratch:

In[22]:= ClearAll[When]
SetAttributes[When, HoldAllComplete]
When[cond_, expr_] := expr /; cond

In[25]:= f = When[NumberQ[x], ToString[x]]
Out[25]= When[NumberQ[x], ToString[x]]

In[26]:= x = 5
Out[26]= 5

In[27]:= f
Out[27]= When[NumberQ[x], ToString[x]]

In[28]:= When[NumberQ[x], ToString[x]]
Out[28]= "5"

Okay, so you can define f using SetDelayed and then it would be reevaluate each
time.  But then you could have just as easily written f[x_] /; NumberQ[x] :=

So is your claim that for interactive use, having f evaluate to the explict
condition statement is more useful than simply returning unevluated?  I can see
that, but I personally don't feel I have been missing such functionality.

Note also that for interactive use Dynamic may be a better choice, as it will
instantly update (although I suppose it won't actually show the conditions
ever, so may not be what you're looking for). 

I hope this helps. 
Itai Seggev
Mathematica Algorithms R&D

  • Prev by Date: Re: What's in an expression?
  • 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?