Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.math.mathematica

Topic: Do we need a When function?
Replies: 5   Last Post: Apr 13, 2014 5:26 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
David Bailey

Posts: 714
Registered: 11/7/08
Do we need a When function?
Posted: Mar 8, 2014 2:42 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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




Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.