Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Setting Attributes for Function Generated Parameters

  • To: mathgroup at smc.vnet.net
  • Subject: [mg110282] Re: Setting Attributes for Function Generated Parameters
  • From: Bob Hanlon <hanlonr at cox.net>
  • Date: Fri, 11 Jun 2010 07:58:23 -0400 (EDT)

shares[n_Integer?Positive, s_Symbol: s] :=
 Module[{var},
  var = Table[
    Symbol[ToString[s] <> ToString[i]],
    {i, n}];
  SetAttributes[#, Constant] & /@ var;
  var]

shares[3]

{s1,s2,s3}

Attributes /@ %

{{Constant}, {Constant}, {Constant}}


Bob Hanlon

---- telefunkenvf14 <rgorka at gmail.com> wrote: 

=============
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?
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?

In[5]:= setConstants[shares[2]]
In[6]:= Attributes[s1]
In[7]:= Attributes[s2]

Out[6]= {Constant}
Out[7]= {Constant}

-RG



  • Prev by Date: Re: O'Reilly Ebook Deal of Day: Mathematica Cookbook ($9.99)
  • Next by Date: Re: Setting Attributes for Function Generated Parameters
  • Previous by thread: LogicalExpand and BooleanConvert
  • Next by thread: Re: Setting Attributes for Function Generated Parameters