MathGroup Archive 2014

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

Search the Archive

Re: Do we need a When function?

  • To: mathgroup at
  • Subject: [mg132401] Re: Do we need a When function?
  • From: Itai Seggev <itais at>
  • Date: Mon, 10 Mar 2014 04:36:53 -0400 (EDT)
  • Delivered-to:
  • Delivered-to:
  • Delivered-to:
  • Delivered-to:
  • References: <>

On Sat, Mar 08, 2014 at 02:42:31AM -0500, 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.

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.
> 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'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

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