MathGroup Archive 1999

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

Search the Archive

Re: Block vs. Module

  • To: mathgroup at
  • Subject: [mg15655] Re: [mg15590] Block vs. Module
  • From: "Fred Simons" <wsgbfs at>
  • Date: Sat, 30 Jan 1999 04:28:49 -0500 (EST)
  • In-reply-to: <>
  • Sender: owner-wri-mathgroup at


When using Block, there might be an interaction between the local 
variables defined in Block and the variables you use yourself. Have a 
look at the following example:

 In[1]:= f[x_] := Block[ {i}, Sum[x^i, {i, 1, 5} ]]

In[2]:= f[a]

Out[2]= \!\(a + a\^2 + a\^3 + a\^4 + a\^5\)

In[3]:= f[i]

Out[3]= 3413

In this situation I would like to see a polynomial in i instead of a 
number. With Module, this cannot happen because Module creates names 
for the local variables that the user is supposed not to use:

In[4]:= g[x_] := Module[ {i}, Sum[x^i, {i, 1, 5} ]]

In[5]:= g[a]

Out[5]= \!\(a + a\^2 + a\^3 + a\^4 + a\^5\)

In[6]:= g[i]

Out[6]= \!\(i + i\^2 + i\^3 + i\^4 + i\^5\)

If you are only interested in variables that have numerical values,  you
can safely use Block instead of Module.

Hope this helps,

Fred Simons
Eindhoven University of Technology

  • Prev by Date: Re: How to construct all possible orderings
  • Next by Date: Re: How to construct all possible orderings
  • Previous by thread: Block vs. Module
  • Next by thread: Re: Block vs. Module