Global variables

C.Reinke C.Reinke@ukc.ac.uk
Thu, 29 Nov 2001 18:56:39 +0000


> Hello, I am interested in using global variables (in GHC).  I need a
> variable to store list of Integers to store temporary results.  I
> have been reading the module MVar, but I wonder if there is an
> alternative way of doing it.  I have already implemented my function
> using an auxiliar argument where I put my lists of Integers.  Will
> the use of a global variable improve my function?

no!-) ah, well, perhaps..

As you've already got your function, using auxiliary arguments, you
probably don't really need to re-write it in a less functional style.

But you will have noticed that much of your code repeatedly does the
same thing - passing the auxiliary around. It's good functional
programming practice to abstract away repeated code (both for reuse
and to get more concise code). One way to do that will lead you into
monads (your function is a state transformer, transforming the state
of the auxiliary list at each step), still a functional solution.

If you really, and absolutely want and must use global, mutable
variables in a functional language, you might find a recent paper
by John Hughes helpful, on that very topic. See his home page:

  http://www.cs.chalmers.se/~rjmh/

Hth,
Claus