Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

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

Search the Archive

Scoping with Module

  • To: mathgroup at smc.vnet.net
  • Subject: [mg109585] Scoping with Module
  • From: Patrick Scheibe <pscheibe at trm.uni-leipzig.de>
  • Date: Thu, 6 May 2010 04:52:49 -0400 (EDT)

Hi,

I cannot explain the following behaviour of Module. Maybe someone else  
can:

in the global scope

f[t_] = t;
DownValues[f]

results in {HoldPattern[f[t_]] :> t} as expected. Simply wrapping it  
with Module, without
making f lokal does not change this behaviour.

Module[{},
  f[t_] = t;
  DownValues[f]
  ]

results still in {HoldPattern[f[t_]] :> t}.
Say I want to replace the right side of the definition with a rule

r = {t :> 1};
f[t_] = t /. r;
DownValues[f]

and we get {HoldPattern[f[t_]] :> 1}

Module[{},
  r = {t :> 1};
  f[t_] = t /. r;
  DownValues[f]
  ]

still the expected result {HoldPattern[f[t_]] :> 1}

Now (sorry for the lengthy introduction) making r lokal

Module[{r},
  r = {t :> 1};
  f[t_] = t /. r;
  DownValues[f]
  ]

and we get {HoldPattern[f[t$_]] :> t$}. The Pattern t_ was renamed  
before the replacement could act.
Using the local variable r to set a global r2 and it's working again.

Module[{r},
  r = {t :> 1};
  r2 = r;
  f[t_] = t /. r2;
  DownValues[f]
  ]

So there must be at least one thing about Module I did'nt understand.  
I thought the main difference between Block
and Module is the lexical scoping!? But the only thing which was  
renamed is the lokal usage of r.
Any ideas?

Cheers
Patrick

7.0 for Mac OS X x86 (64-bit) (February 19, 2009)


  • Prev by Date: FrontEnd and bash automatization
  • Next by Date: Re: Parallelize NonlinearModelFit
  • Previous by thread: Re: FrontEnd and bash automatization
  • Next by thread: Re: Scoping with Module