MathGroup Archive 2010

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

Search the Archive

Re: question on passing variable to function by reference

  • To: mathgroup at smc.vnet.net
  • Subject: [mg113581] Re: question on passing variable to function by reference
  • From: Leonid Shifrin <lshifr at gmail.com>
  • Date: Wed, 3 Nov 2010 02:56:05 -0500 (EST)

Hi Sjoerd,

FWIW, I can confirm that this is indeed pass-by-reference. I find this quite
useful at times.
Perhaps, the most useful for me was the ability to pass symbols to functions
which then
define those symbols inside their scope as closures (functions which operate
on internal state of those
functions outside their scope), so that I can later use them in my original
larger function to operate
on the symbols created in a second larger function. This is very useful in
UI programming with Dynamic, for
instance - this way you can split the monolithic UI into more managable
chunks and still
maintain the dynamic updating of the variables. I will be happy to extract
some minimal
example out of my code if you are interested.

Sometimes it also offers algorithmic advantages, if the algorithm is more
suited for
in-place modifications. In this thread

http://groups.google.com/group/comp.soft-sys.math.mathematica/browse_thread/thread/69d53a2ffa5a640e

I gave a pass-by-reference based implementation of qsort, which is
asymptotically better than
the alternative one using immutable lists.

The massive simultaneous assignments using Part, which are very fast, also
use pass-by-reference to gain
the speed which is hard to achieve by other means,and are more
memory-efficient at the same time.

I am also using pass-by-reference  to dynamically generate data structures
(structs),  for example
as described in my post in this recent thread:

http://groups.google.com/group/comp.soft-sys.math.mathematica/browse_thread/thread/b2ddf61da56b0be9

The code there is not exactly transparent, but the idea is simple: if you
have a structure field name
say foo, you can take a symbol foo and give it HoldAll attribute. Then,
expression like

s = With[{var = Unique[]},foo[var]]

or

s = foo[Evaluate[Unique[]]]

can be viewed as the "memory cell", so that you can store some information
in it. In particular,
s[[1]]=smth will assign a new value, and s[[1]] will return the currently
stored value.

I am sure there are other uses for pass-by-reference mechanism, these are
just some off the top of
my head, in case if you are interested.

Now, regarding the check for list being a list in the OP situation.There
will be a problem with such check
if the passed argument is stored in a variable (so that what is passed is a
variable rather than literal list),
and if the check is implemented straightforwardly as _List, due to the
interplay between pattern-matching
and evaluation. There was a question on the group on a very similar setting
about a year ago:

http://groups.google.com/group/comp.soft-sys.math.mathematica/browse_thread/thread/c062579f93e093a6

and I also discussed this topic in my book at

http://www.mathprogramming-intro.org/book/node408.html

where I consider this situation in greater detail.


Cheers,
Leonid



On Tue, Nov 2, 2010 at 1:01 PM, Sjoerd C. de Vries <
sjoerd.c.devries at gmail.com> wrote:

> Actually, I thought you couldn't do a 'pass by reference' in
> Mathematica, but this really looks like the real thing.
>
> You might want to add a test to check that list is really a List, but
> otherwise I don't see a problem.
>
> Cheers -- Sjoerd
>
> On Nov 1, 11:01 am, "Nasser M. Abbasi" <n... at 12000.org> wrote:
> > Suppose I want to pass an array to function to be filled in by some
> value=
>


  • Prev by Date: Re: Balance point of a solid
  • Next by Date: Adding labels to a GraphicsGrid object (2, with code)
  • Previous by thread: Re: question on passing variable to function by reference
  • Next by thread: Re: question on passing variable to function by reference