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: [mg132403] Re: Do we need a When function?
  • From: Szabolcs HorvÃt <szhorvat at gmail.com>
  • Date: Mon, 10 Mar 2014 04:37:33 -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: <lfehl0$5kq$1@smc.vnet.net>

On 2014-3-8, 2:42 , David Bailey wrote:
> Dear All,
>
> Recently I wanted to create an expression that only evaluated when x was
> a number. 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.
>
> More generally, it would be nice to write an expression that will only
> evaluate when an expression is true. I solved that by writing a function
> When:
>
> SetAttributes[When, HoldAll];
> When[cond_, val_] := val /; cond
>
> However, my point is that this construction is sufficiently useful that
> it should be built in to Mathematica, and my solution might not be
> obvious to all users.
>
> Am I missing a simpler solution?
>
> Do others agree that this is an omission in the language?
>

I see where you are coming from with this.

I think this comes up most often in cases like this:

http://support.wolfram.com/kb/3820

When[] would make it unnecessary to define a separate function.

However one might argue that in situations like this it is better 
practice to define that function anyway, if not for anything else then 
just for structuring the code better (which could avoid errors).

Do you see other common use cases for it?




  • Prev by Date: Re: matrix manipulation
  • Next by Date: Re: matrix manipulation
  • Previous by thread: Re: Do we need a When function?
  • Next by thread: Re: Do we need a When function?