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.
mysquare[x_] :=
Module[{n = Catch[
(*Your code goes here*)
If[x < 0, Throw[stop]];
a=Sqrt[x];
If[x == 7, Throw[stop]];
a
]},
n /; ! (n === stop)
]
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:
SetAttributes[noException, HoldAll]
noException[f_[a_, b_]] /; (f === SetDelayed) := (SetDelayed[a,
Module[{n = Catch[b]}, n /; ! (n === stop)]])
Meaning you could carry out the above definition as:
noException[mysquare[x_]:=(
If[x < 0, Throw[stop]];
a=Sqrt[x];
If[x == 7, Throw[stop]];
a
)]
The parenthasis on the right hand of the definition is needed because of the way compound expressions is parsed, but can be omitted with a refined definition:
noException2[
CompoundExpression[f_[a_, b_], c__]] /; (f === SetDelayed) :=
(SetDelayed[a, Module[{n = Catch[CompoundExpression[b, c]]}, n /; ! (n === stop)]])
Which corrects the error if you forget the parenthasis.
F[x_?Positive] := Sqrt[x]– chris Oct 29 '12 at 08:31F[x_] := If[x >= 0, Sqrt[x], Defer[F[x]]];– chris Oct 29 '12 at 08:55F[a](returnsIf[a >= 0, Sqrt[a], F[a]]but should returnF[a]) – Suzan Cioc Oct 29 '12 at 09:01F[x_] := If[x >= 0, Sqrt[x], Defer[F[x]], Defer[F[x]]]– chris Oct 29 '12 at 09:03Condition? – Suzan Cioc Oct 29 '12 at 09:06Conditionrather thanIfbut since you wantedIf– chris Oct 29 '12 at 09:08Conditionhas a disadvantage that in complex situations I need to do the job twice: once while checking if function is applicable and secondly when calculating it. – Suzan Cioc Oct 29 '12 at 09:19$Failed, which you can use instead of yourFail[]. – J. M.'s missing motivation Oct 29 '12 at 10:23$Failedfor that purpose? – Suzan Cioc Oct 29 '12 at 10:49Sqrtfunction defined for positive numbers. – Suzan Cioc Oct 29 '12 at 14:34RuleCondition(which is internally called by condition) hasHoldFirst. Look at the trace and you will understand. – sebhofer Oct 29 '12 at 15:57