Re: A riddle: Functions that return unevaluated when they cannot
- To: mathgroup at smc.vnet.net
- Subject: [mg82484] Re: A riddle: Functions that return unevaluated when they cannot
- From: Albert <awnl at arcor.net>
- Date: Mon, 22 Oct 2007 05:34:35 -0400 (EDT)
- References: <ff9sin$5vc$1@smc.vnet.net> <ffckoe$sop$1@smc.vnet.net> <ffed3p$bg7$1@smc.vnet.net>
Hi,
Is this what you are looking for?
In[27]:= ClearAll[g]
In[28]:= g[x_]:=Module[{res=x+2},
res/;res<10
]
In[29]:= g[15]
Out[29]= g[15]
In[30]:= g[1]
Out[30]= 3
That is, using a conditional on your last expression decides whether or
not a return value is returned or the input expression is returned
unevaluated. This could of course be a flag which tells whether your
calculation succeeded or not.
I can't remember where I found this trick, I don't know whether it is
documented and how/why it works. But I think it is used in the code of
some of the Standard Packages (and probably in the functions you
mentioned as well) so should work reliable, but I have no guarantees for
that...
hth,
albert