Re: How to short-circuit match failure?
- To: mathgroup at smc.vnet.net
- Subject: [mg114477] Re: How to short-circuit match failure?
- From: kj <no.email at please.post>
- Date: Sun, 5 Dec 2010 21:53:00 -0500 (EST)
- References: <id7t63$lc6$1@smc.vnet.net>
My previous post proposed this definition: > In[16]:= > ... > > Module[{chkargs, core}, > foo[args___] := ( > chkargs[x_List /; Length[x] < 3, y_] := True; > chkargs[_List, _] := Message[foo::toolong]; > chkargs[_, _] := Message[foo::nolist]; > chkargs[x___] := Message[foo::nargs, Length[{x}]]; > core[x_, y_] := {#, y} & /@ x; > core[args] /; chkargs[args]) > ] Sorry, that's grossly inefficient. There's no need to reset chkargs and core every time foo is evaluated. This is more reasonable: In[16]:= ... Module[{chkargs, core}, chkargs[x_List /; Length[x] < 3, y_] := True; chkargs[_List, _] := Message[foo::toolong]; chkargs[_, _] := Message[foo::nolist]; chkargs[x___] := Message[foo::nargs, Length[{x}]]; core[x_, y_] := {#, y} & /@ x; foo[args___ /; chkargs[args]] := core[args]; ] ~kj