Do we need a When function?
- To: mathgroup at smc.vnet.net
- Subject: [mg132393] Do we need a When function?
- From: David Bailey <dave at removedbailey.co.uk>
- Date: Sat, 8 Mar 2014 02:42:31 -0500 (EST)
- Delivered-to: firstname.lastname@example.org
- Delivered-to: email@example.com
- Delivered-to: firstname.lastname@example.org
- Delivered-to: email@example.com
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? David Bailey http://www.dbaileyconsultancy.co.uk