Tim Bishop wrote:
>
> I am relatively new to Haskell, and I'm using Hugs 1.4.
>
> My my source of programming is Java, with the odd bit of basic thrown in for
> good measure.
>
> Now one of the first things I notice about Haskell is that there don't seem
> to be variables in the same sense that there are in other programming
> languages. I want to be able to store a value, or a set of values for use
> through-out various functions.
>
> Any suggestions ?
As You noticed right, in pure functional programming there is no concept
of assignment at all, i.e. global variables representing a storage
location. Variables are always meant to stand for the same value in
every place (context) if occuring inside the same scope.
If You want to use these values in a couple of functions You have to
pass them as arguments. There are two ways worth to be considered:
1. Pass each value as a separate parameter and make use of currying if
possible. E.g. counter has to be passed from f to g:
f x counter = g (h x) counter
You could hide propagating counter by writing f as
f x = g (h x)
Choosing the right position for the parameter is essential here, i.e. f
counter x = ... is a perhaps bad choice for the definition of f.
2. If there are many values to be passed around, You possibly wish to
collect them in a data structure, preferably a record. This record may
represent the environment of Your computation. You can pass it
explicitely to Your functions or use a monad to hide the environment
passing. In the example above also x is passed from f to g, so the
environment might consist of a counter and a x-coordinate, e.g.
data Env = Env { counter :: Int, x :: Int }
and with explicit passing
f env = g (h (x env)) (counter env)
Matthias Mann
University of Frankfurt/Main, Germany