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

MathGroup Archive 2010

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

Search the Archive

Re: looping


Richard Fateman wrote:
> Andrzej Kozlowski wrote:
>> On 25 Jan 2010, at 11:08, Leonid Shifrin wrote:
>>
>>> Besides, changes to this seem unlikely because this
>>> is a fundamental mechanism behind scoping in Module, and it has been this
>>> way for a very long time (I don't know since which version but I wouldn't be
>>> surprised if it  was there already in version 1).
>> Actually there was no Module in version 1. (e.g. see the documentation for Module). Module was introduced in Version 2. Version 1 had only Block.
>>
>> Andrzej Kozlowski
>>
>> (Nice tricks, by the way. I have know about some of them for a long time but others are new to me.)
>>
> Oh dear.  As I pointed out in 1992, the Module mechanism is flawed.
> Module makes up names,
> 
> as you can see by  g1[x_]:=Module[{r}, r]
> 
> with dollar signs.  e.g. I got  r$592  one time.
> 
> The standard advice is you can avoid conflict yourself by not using 
> names with dollar signs in them.
> 
>   Unfortunately, if you have several independent programs perhaps 
> written by different people and they each generate persistent names via 
>   Module[{...,r...} ...] and you load them into the same system  then 
> THEY can conflict.
> 
> consider
> g1[x_]:=Module[{r},If[NumberQ[A[r]],Print[r],A[r]=0]]
> 
> run it a bunch of times.  Save g1 and A in file f1
> start up a new system
> define
> g2[x_]:=Module[{r},If[NumberQ[A[r]],Print[r],A[r]=1]]
> 
> run it a bunch of times.  Save g2 and A in file f2.
> start up a new system
> 
> load files f1 and f2.
> 
> the behavior of g1 and g2 will be, so far as I can tell, dependent on
> the (random?) setting of some inaccessible counter in Mathematica.
> 
> 
> 
> 
> 

Clearly, Module should not be used to generate names that will be saved 
and loaded into another Mathematica session. If you REALLY need several 
people to independent people to generate such names without getting 
clashes, it might be better to generate names based on AbsoluteTime and 
$UserName/$LicenseID/$MachineID as appropriate.

To describe the Module mechanism as 'flawed' because you can't use it in 
this way, seems totally unreasonable - akin to describing real 
arithmetic as 'flawed' because it can't represent Sqrt[-1]!

One of the nice features of Module, is that the method of localising 
names is totally explicit - so that it is easy to work out what is going 
on is subtle cases.

David Bailey
http://www.dbaileyconsultancy.co.uk


  • References:
    • looping
      • From: glenn kuhaneck <mcguyver128@yahoo.com>
    • Re: Re: looping
      • From: Leonid Shifrin <lshifr@gmail.com>
  • Prev by Date: Re: solve partial differential equations with mathematica
  • Next by Date: Re: A question about location of points on a circle
  • Previous by thread: Re: looping
  • Next by thread: Re: looping