MathGroup Archive 2001

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

Search the Archive

Re: Is this a bug?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg32096] Re: Is this a bug?
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Sun, 23 Dec 2001 03:52:35 -0500 (EST)
  • References: <9vs8ph$g1d$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Paul,

1


Here is a version of Increment that seems to be close to the built in one,
(follow the latter's evaluation with TracePrint, there is no evidence of the
use of With in the built in function  but the main evaluation steps are the
same).

    Attributes[incr]={HoldFirst};  (*Increment is HoldFirst*)
    incr[x_]:= With[{t=x},x=t+1;t]

Compare

    k[x_]:=Module[{},Print["hallo world"];x];

    a[5]=a5;
    incr[a[k[5]]]

        hallo world
        hallo world

        a5

Explanation:
Because of incr being HoldFirst the main steps in the evaluation of
incr[a[k[5]]] are as follows

    With[{t=a[k[5]]},a[k[5]]=t+1;t]
    With[{t=a[5]},a[k[5]]=t+1;t] (*+ first print *)
    With[{t=a5},a[k[5]]=t+1;t]
    a[k[5]]=a5+1;a5 (*+ second print *)
    a[5] = a5+1;


2

Let's modify Random so that we get a printout of what the selections are

    ri:=(Print[#];#)&[ Random[Integer]]

    a[0]:=a0;a[1]:=a1;
    Do[ a[ri]++;Print[{a[0],a[1]}], {3} ];

        0
        0
        {1 + a0, a1}
        0
        1
        {1 + a0, 2 + a0}
        1
        0
        {3 + a0, 2 + a0}

Following the code in 1 above successive evaluations of the body of Do[..]
give the assignments
    a[0] = a[0]+1 = a0+1
    a[1] = a[0]+1 = (a0+1) +1 = a0+2
    a[0] = a[1]+1 = (a0+2)+1 = a0

The general situation is that an evaluation of the body that begins by
giving the printouts
         i
         j
will make the assignment
    a[j] = a[i]

Here is another example

    a[0]:=a0;a[1]:=a1;
    Do[ a[ri]++;Print[{a[0],a[1]}], {3} ];

        0
        1
        {a0, 1 + a0}
        0
        1
        {a0, 1 + a0}
        0
        1
        {a0, 1 + a0}

--
Allan

---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565


"Paul van Wamelen" <wamelen at math.lsu.edu> wrote in message
news:9vs8ph$g1d$1 at smc.vnet.net...
> 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!
>
>
>




  • Prev by Date: Re: Displaying Mathematica's Global rules
  • Next by Date: Find many Roots
  • Previous by thread: Re: Re: Is this a bug?
  • Next by thread: Re: Re: Is this a bug?