Re: iterator strings as lists?
- To: mathgroup at smc.vnet.net
- Subject: [mg3564] Re: iterator strings as lists?
- From: villegas (Robert Villegas)
- Date: Mon, 25 Mar 1996 21:33:28 -0500
- Organization: Wolfram Research, Inc.
- Sender: owner-wri-mathgroup at wolfram.com
[Dave Wagner]: > I don't know what the design rationale for this is. It seems to > me like Table could get by with the HoldFirst attribute, but maybe > there's some pathological case of which we're not aware that precludes > this. [me]: > A non-pathological case is random limits of iteration: > > In[3]:= Table[0, {Random[Integer, {0, 5}]}, {Random[Integer, {0, 5}]}] > > Out[3]= {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0}} > > In[4]:= Table[0, {Random[Integer, {0, 5}]}, {Random[Integer, {0, 5}]}] > > Out[4]= {{0, 0, 0, 0, 0}, {}, {0, 0}, {0, 0, 0}} Actually, another reason, documented in the book, is that the index variables are protected from global values during the Table (the same type of scoping Block uses). So if you've assigned values to x and y, In[1]:= {x, y} = {4, 3}; you can still construct tables using x and y as iteration variables: In[2]:= Table[f[x, y], {x, 3}, {y, 4}] Out[2]= {{f[1, 1], f[1, 2], f[1, 3], f[1, 4]}, > {f[2, 1], f[2, 2], f[2, 3], f[2, 4]}, > {f[3, 1], f[3, 2], f[3, 3], f[3, 4]}} If arguments beyond the first will pre-evaluated, then this wouldn't work: In[3]:= Attributes[Table] = Attributes[Table] /. HoldAll -> HoldFirst; In[4]:= Table[f[x, y], {x, 3}, {y, 4}] Table::itraw: Raw object 4 cannot be used as an iterator. Out[4]= Table[f[x, y], {4, 3}, {3, 4}] Robby Villegas ==== [MESSAGE SEPARATOR] ====