RE: Holding Arguments in a Second Argument List
- To: mathgroup at smc.vnet.net
- Subject: [mg32295] RE: [mg32238] Holding Arguments in a Second Argument List
- From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.de>
- Date: Fri, 11 Jan 2002 04:35:33 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
Dear David, I remember this question had already come up in MathGroup, but I can't find my answer to it on any of my disks. Here I report on the same idea, possibly in better guise. Please see below: > -----Original Message----- > From: David Park [mailto:djmp at earthlink.net] To: mathgroup at smc.vnet.net > Sent: Wednesday, January 09, 2002 9:18 AM > To: mathgroup at smc.vnet.net > Subject: [mg32295] [mg32238] Holding Arguments in a Second Argument List > > > Dear MathGroup, > > I have a series of procedures which manipulate a matrix. I > like to define > them in this fashion. > > function[args___][matrix_] := ... because then I can write lines like > > matrix // function[args...] etc. > > But I have to define the function so that matrix is held, and > I don't want > to write Unevaluated with every use. But Attributes = {HoldFirst} only > applies to the first argument list. Is there a direct way to > define the > attributes of a second argument list? > > In any case, I write the routines this way. > > function[args___]:= > Function[matrix, > Module[{}, code], > {HoldFirst}] > > and that works. The code usually changes the matrix and I display it > separately. But sometimes the operation will fail and then I > would like > > function[args...][matrix] > > to return $Failed. But I am unable to do that. The best I > seem to be able to > do is return Return[$Failed]. > > Here is a sample routine which just tries to return $Failed. > > foo[mathgroup_] := > Function[mat, > Module[{}, > If[Not[mathgroup === great], Return[$Failed]]], > {HoldFirst}] > > foo[x][mat] > Return[$Failed] > > Can anyone tell me how to get rid of the Return wrapper? > > Thanks > > David Park > djmp at earthlink.net > http://home.earthlink.net/~djmp/ > > This is my test example, it works with DownValues: In[1]:= SetAttributes[g, HoldAll] In[2]:= g[matrix_] := Dimensions[Unevaluated[matrix]] In[3]:= g[{Print["ho"], Print["and lo"]; Unevaluated[Sequence[]]}] Out[3]= {2} Nothing printed and matrix has not collapsed. It doesn't work with SubValues: In[4]:= SetAttributes[f, HoldAll] In[5]:= f[args___][matrix_] := Dimensions[Unevaluated[matrix]] In[6]:= f[1][{Print["ho"], Print["and lo"]; Unevaluated[Sequence[]]}] >From In[6]:= "ho" >From In[6]:= "and lo" Out[6]= {1} The trick now is to introduce an intermediate Symbol g1 here which is given the Hold Attribute: In[8]:= SetAttributes[g1, HoldAll] In[9]:= g1[args___, matrix_] := Dimensions[Unevaluated[matrix]] In[10]:= g1[1, {Print["ho"], Print["and lo"]; Unevaluated[Sequence[]]}] Out[10]= {2} It works of course; now the definition for f (called f1 here), and no Attributes needed for that one. In[11]:= f1[args___] := Sequence[Null, g1[args, #], {HoldAll}] & (Just for joking, we normally write up this differently.) Now you have got what you want: In[13]:= {Print["ho"], Print["and lo"]; Unevaluated[Sequence[]]} // f1[1] Out[13]= {2} Yours, Hartmut