Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2001
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2001

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

Search the Archive

Re: Is this a bug?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg32068] Re: [mg32047] Is this a bug?
  • From: Andrzej Kozlowski <andrzej at tuins.ac.jp>
  • Date: Fri, 21 Dec 2001 03:57:51 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

I guess I misunderstood you. I meant only to explain that the way your 
program worked was not a bug. If you really want to achieve your aim 
(single evaluation of k[5]) you have to use a different approach. There 
is no reason why it has to be done the same way as in another language, 
e.g. C. For example, in mathematica you can do it as follows:

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

In[2]:=
a[5] = 0;

In[3]:=
a[k[5]]++

 From In[3]:=
hallo world

Out[3]=
0

Of course there is a price to be paid, as you keep using k, Mathematica 
will remember more and more values. But then nothing comes for free and 
if yu really think you need C that's what you should use.

Andrzej Kozlowski
Toyama International University
JAPAN
http://platon.c.u-tokyo.ac.jp/andrzej/



On Thursday, December 20, 2001, at 05:42  PM, 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!
>
>
>
>
>
JAPAN
http://platon.c.u-tokyo.ac.jp/andrzej/



  • Prev by Date: Re: Is this a bug?
  • Next by Date: Re: Is this a bug?
  • Previous by thread: Re: Is this a bug?
  • Next by thread: Re: Is this a bug?