       Re: Do we need a When function?

• To: mathgroup at smc.vnet.net
• Subject: [mg132433] Re: Do we need a When function?
• From: Itai Seggev <itais at wolfram.com>
• Date: Sat, 15 Mar 2014 03:45:25 -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>

```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:= ClearAll[When]
SetAttributes[When, HoldAllComplete]
When[cond_, expr_] := expr /; cond

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

In:= x = 5
Out= 5

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

In:= When[NumberQ[x], ToString[x]]
Out= "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] :=
ToString[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
217-398-0700

```

• 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?