Re: PrograMing: match without evaluation
- To: mathgroup@smc.vnet.net
- Subject: [mg10262] Re: PrograMing: match without evaluation
- From: Allan Hayes <hay@haystack.demon.co.uk>
- Date: Sat, 3 Jan 1998 05:07:15 -0500
- References: <684v40$q8m@smc.vnet.net> <68crke$a2v@smc.vnet.net>
Xah wrote:
> How can I define a function f such that when f is matched, certain
> actons are taken and the input is returned unevaluated?
Xah:
I find the following form useful. Coupled with using Throw and Catch it
allows action to be taken as the function is being evaluated as well as
allowing input checking (possibly using Hold attributes on the left) -
it can be combined with checks on the left which may catch a problem
more quickly .
foo:= With[{result = foointernal} , result./; result =!= $Failed]
Define foointernal to give $Failed, perhaps after issuing a message,
when foo is to be returned unevaluted and to give the intended result
otherwise. If $Failed can be an intended result then it will have to
be replace by something else.
An alternative that avoids the last problem is
foo:= Module[{fail, result}, result/; foointernal]
For example
f[x_] := Module[{fail, result, succeed}, result/; If[x>6, fail, result =
x^2; succeed] === succeed]
Allan
--
Allan HayesTraining and Consulting
Leicester, UK
hay@haystack.demon.co.uk
http://www.haystack.demon.co.uk
voice: +44 (0)116 271 4198
fax: +44 (0)116 271 8642