Mathematica 9 is now available
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: Memory Conservation

  • To: mathgroup at smc.vnet.net
  • Subject: [mg29922] Re: Memory Conservation
  • From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
  • Date: Thu, 19 Jul 2001 03:56:51 -0400 (EDT)
  • Organization: Universitaet Leipzig
  • References: <9iomg8$i13$1@smc.vnet.net> <9j3a5e$in4$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Hi,

Orestis Vantzos wrote:
> 
> a) The main difference between Module and Block is just that; Module removes
> all local variables.

That is nonsense! Module[] has variables with *local* scope. That means,
that for

f1[x_]:=Module[{y},f2[x]]

f2[x_]:=Module[{x2=x*x},x*x*x2]

the function f2 can't access/modify the variable y defined in the
Module[] of
f2. That is the usual behaviour of programming languages 
(with exeption of LISP).

With:

f1[x_]:=Block[{y},f2[x]]

f2[x_]:=Module[{x2=x*x},x*x*x2]

f2[] *can* access and modify the variable y. 
That is called dynamic scope.
Every function that is called form a Block[] has access to the Block[]
variables.
This sounds a bit strange but it is very useful. 
Here is an example
of a function that transform a polynom due to Horners rule

makeHorner[x_] := Fold[#2 + x*#1 &, 0, Reverse[coeff]]

PolynomToHorner[p_, x_] /; PolynomialQ[p, x] :=
  Block[{coeff},
    coeff = CoefficientList[p, x];
    makeHorner[x]
    ]

The makeHorner[] function don't need the coefficents as 
an function argument. This is a way to avoid, that large
data copyed to Mathematica's stack. It will usual faster
than Module[] and save stack memory.

*Both* Block[] and Module[] will clean the memory
on return ! The local scope of Module[] is not so clean
implemented as it should. Module[{y},...] will create
a variable y$?  where "?" is a number that changes for
every call of the Module[]. Since y$? is an other symbol
for ervery Module[] execution the called function can't
get the true symbol name if it is not passed as an argument.

BTW: Orestis ! please look into the manuals *before* you post
a false statement.

Regards
  Jens


  • Prev by Date: Re: Thickness Isn't Thickness
  • Next by Date: parametric plots
  • Previous by thread: Re: Memory Conservation
  • Next by thread: weighting CA neighbors by distance