Re: Setting Attributes for Function Generated Parameters (with package
- To: mathgroup at smc.vnet.net
- Subject: [mg110280] Re: Setting Attributes for Function Generated Parameters (with package
- From: Albert Retey <awnl at gmx-topmail.de>
- Date: Fri, 11 Jun 2010 07:58:01 -0400 (EDT)
- References: <husjvd$6ht$1@smc.vnet.net>
Am 11.06.2010 08:09, schrieb telefunkenvf14: > Group: > > My goal is to generate a list of symbolic parameters that other > functions in a package can Map[] over. For sake of example, suppose I > define the following 'utility' function as part of a larger package. > > In[1]:= ClearAll["Global`*"] > In[2]:= shares[n_?IntegerQ/;n>0,s_Symbol: > s]:=Module[{i},Table[Symbol[ToString[s]<>ToString[i]],{i,n}]] > In[3]:= shares[2] > > Out[3]= {s1,s2} > > I would now like to include the attribute Constant to each of the > s1, ..., sn generated. What is the best way to do this? I'm not sure whether it is the best way, but this is one (obvious) way: shares[n_?IntegerQ /; n > 0, s_Symbol: s] := Module[{symbols}, symbols = Table[Symbol[ToString[s] <> ToString[i]], {i, n}]; Scan[SetAttributes[#, Constant] &, symbols]; symbols ] note that it actually is not necessary to localize i with Module, since Table itself is a scoping construct. > My gut tells me that ideally I'd want to do this inside the shares[] > function itself, but I couldn't figure out how. The workaround I've > been toying with is to define another utility function: > > In[4]:= setConstants[vars_]:=SetAttributes[#,Constant]&@vars > > This seems to work ok...But does anyone see potential pitfalls with > this approach? I think there is only one potential pitfall: you could forget to call the second utility function and use the symbols before they have the Constant attributes, making them misbehave. > In[5]:= setConstants[shares[2]] > In[6]:= Attributes[s1] > In[7]:= Attributes[s2] > > Out[6]= {Constant} > Out[7]= {Constant} > hth, albert