@SuzanCioc Could you provide a concise example of your case with double application?
–
jVincentOct 29 '12 at 9:49

2

@SuzanCioc Your examples does not illustrate the "complex situation" you mention in your comment. You can perfectly well do everything you ask using condition. If you have other cases where condition will not work, you could include it in you example to show why condition is not suitable for you.
–
jVincentOct 29 '12 at 12:04

2

I don't know why this has been down-voted three times. This is a good question. It may not have been asked in the best way but it would be hard to ask it better without first knowing the answer, IMHO.
–
Mr.Wizard♦Oct 29 '12 at 19:16

You should be able to tell the difference. In the working code the code that sets fail is evaluated before Condition is evaluated. In your code, Condition is evaluated first, which holds the evaluation of the code. This is because condition has Attributes HoldAll.

Sorry don't catch the difference between I wrote and you wrote?
–
Suzan CiocOct 29 '12 at 16:19

You mean I pass entire answer to condition while you assigned it to a variable first?
–
Suzan CiocOct 29 '12 at 16:20

Ok, this looks like a final and sufficient solution I was looking for. It both uses condition and procedural. So I was wrong avoiding condition at all and people were wrong forcing condition in normal form.
–
Suzan CiocOct 29 '12 at 16:23

@SuzanCioc I added a comparison. It's because Condition[a=2,b] only evaluates the a=2 part if b is True. So the code you use to calculate fail has to be outside of Condition.
–
jVincentOct 29 '12 at 16:24

If test is set to False, the Condition fails and Mathematica pretends that the match to f[x_List] never happened, moving on to try other rules for f. This is a little-known tidbit of Mathematica programming. Note that Condition must be the last statement in the Module (or Block or CompoundExpression) for it to work.

But this is only a small part of this fine material. For instance, to return an error message, which contains the unevaluated function call, he give the following implementation

Yes, it is possible. However, it requires the use of the undocumented System`Private`$Localized, which was (as far as I know) first discovered by @Rojo. This symbol is most likely an internal implementation detail of the evaluator and, being located in a context that is obviously not meant for manipulation by the user, should be approached with caution.

I'm going to go out on a limb here and just assume that your problem is that you are nesting conditionals inside your code definitions, and that deeper levels are ignored. If that is the case, what you could do is to use a conditional check to see if your code processed without throwing exceptions, and use exceptions as your Fail[] function.

The you can halt the computation at different locations along the code and let it stay in an unevaluated form:
mysquare[-2] returns mysquare[-2], mysquare[4] returns 2, and mysquare[7] will evaluate Sqrt[7] but return mysquare[7].

If this is close to what you are actually doing, I would suggest adding some tag to the objects you pass along, that signify that they where already evaluated and didn't change. Otherwise you will end up reassessing that they should remain unevaluated every time you pass them along. This problem can be seen for instance calling Trace[mysquare[7] // Sin] where the calculation needed to assert that mysquare should not evaluate will run twice.

Update

So just for kicks I tried to encapsulate this in a function, but since you need the condtional to wrap the code, I belive that it is not possible to keep a function only on the right hand side to carry this out (Someone correct me please if this can actually be done!). So what I did was create a function that transforms a function definition to include the Exception check condition:

You are still using a Condition, but in special form. May be it can be used easier, without throwing? See my update.
–
Suzan CiocOct 29 '12 at 15:47

@SuzanCioc Note that the reason i use throw is to exit the computation as quickly as possible, if for instance you had a very intensive computation If[x<0,a=42;success ==1];Pause[10];a then using the example code you posted, you would always need to compute Pause[10], even when you know early on that success ==0. Using exceptions allow you to short-circuit those cases.
–
jVincentOct 29 '12 at 16:02

This means that both solutions are incomplete since do not fully resemble Condition behavior.
–
Suzan CiocOct 29 '12 at 9:21

@SuzanCioc In what way does chris' solution fail? Also I second jVincent's request from above, I have yet to see a reason why condition does not work for you.
–
sebhoferOct 29 '12 at 12:15

@sebhofer I can't post hundreds lines of code here. But you can trust me that I currently use Condition and I dislike it's behavior.
–
Suzan CiocOct 29 '12 at 13:35

@SuzanCioc That is all very well, but you won't get any help here by saying: "I don't like the behaviour of Condition so give me something I do like". If you want constructive input here, you need to pose a question which clearly describes the problems you are experiencing and what you eventually want to achieve. Your question, frankly, does not fulfill these criteria.
–
sebhoferOct 29 '12 at 13:53

4

@SuzanCioc The downvotes are not due to people not knowing the answer. Trust me on this, this site has some pretty intelligent people. The reason is that you posted a question that reads like this "I want a function that works like List, but I don't like List, so do it without List". You never fully explain why you dislike Condition or what effect it is you are trying to get rid of. Essentially you asked an incomplete question. If you write a small example where the behavior you dislike in Conditions shows up, then people will most likely remove downvotes and be able to provide help.
–
jVincentOct 29 '12 at 14:35

Mathematica is a registered trademark of Wolfram Research, Inc. While the mark is used herein with the limited permission of Wolfram Research, Stack Exchange and this site disclaim all affiliation therewith.