Message Boards Message Boards

local variables

Posted 9 years ago

I need to build functionalities based on Table function.

In[42]:= Table[{u, v}, {u, {1, 2, 3}}, {v, {4, 5}}]

Out[42]= {{{1, 4}, {1, 5}}, {{2, 4}, {2, 5}}, {{3, 4}, {3, 5}}}

So I put it in a function :

f[x_, y_] := Table[{u,v}, {u, x}, {v, y}]

f perfectly works for the previous example:

In[2]:= f[{1, 2, 3}, {4, 5}]

Out[2]= {{{1, 4}, {1, 5}}, {{2, 4}, {2, 5}}, {{3, 4}, {3, 5}}}

but it doesnt work in that case :

In[3]:= f[{a, b, c}, {u, v}]

Out[3]= {{{a, a}, {a, v}}, {{b, b}, {b, v}}, {{c, c}, {c, v}}}

As an evidence, local variable is the point. If I create a function, the first property I expect is encapsulation. I don't need interference with caller symbol space, especially for iterators.

So what is the correct way to make iterators "isolated", no interfering with the parameters content ?


2 Replies


 f[x_, y_] := Module[{u, v}, Table[{u, v}, {u, x}, {v, y}]]
POSTED BY: Ilian Gachevski

I'm looking at the documentation about Module at this time. very instructive...

Thank you very much for your help.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract