Re: Is this a bug?
- To: mathgroup at smc.vnet.net
- Subject: [mg32061] Re: [mg32047] Is this a bug?
- From: Rob Pratt <rpratt at email.unc.edu>
- Date: Fri, 21 Dec 2001 03:57:34 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
On Thu, 20 Dec 2001, Paul van Wamelen wrote: > Is the following a bug? > > Mathematica 4.1 for Sun Solaris > Copyright 1988-2000 Wolfram Research, Inc. > -- Motif graphics initialized -- > > In[1]:= k[x_] := Module[{},Print["hallo world"];x]; > > In[2]:= a[5] = 0; > > In[3]:= a[k[5]]++ > hallo world > hallo world > > Out[3]= 0 > > > I would have expected only one "hallo world" and it would seem to be > more efficient to only evaluate the k[5] once... > > The above example is not important but in the form below it had me > baffled for a while: > > In[4]:= tst[n_] := Module[{a}, > a[0] = a[1] = 0; > Do[a[Random[Integer,{0,1}]]++,{n}]; > {a[0],a[1]}] > > In[5]:= tst[50] > > Out[5]= {24, 24} > > (Does not add up to 50!) > > Thanks! I encountered the second form just two days ago. I'll leave it to somebody else to explain the above behavior, but I ended up using a workaround like the following. tst[n_] := Module[{a, r}, a[0] = a[1] = 0; Do[(r = Random[Integer,{0,1}]; a[r]++), {n}]; {a[0],a[1]}] Rob Pratt Department of Operations Research The University of North Carolina at Chapel Hill rpratt at email.unc.edu http://www.unc.edu/~rpratt/