State Transformer

Albert Lai trebla@vex.net
11 Jan 2002 17:10:16 -0500


Theodore Norvell <theo@cs.ubc.ca> writes:

> Jorge's question raised a question in my mind.  The IOExts
> module has many of the same features as the ST module, why
> are there two ways to do the same thing?  Is the ST module
> only there for legacy purposes?

My user view is that I appreciate the presence of both.

When I write a pure function that can be implemented efficiently in
imperative programming, I want the ST monad to support mutable
variables.  E.g., to implement "f n returns a list of all primes
between 2 and n", I want to use a mutable array and hide it.

When I write an I/O-bound routine that can be implemented conveniently
with state variables, I want the IO monad to support mutable
variables.

Now my grief is that I cannot write a subprogram with state variables
and have it reused in ST and IO.  Fortunately I can write a subprogram
with mutable arrays and have it reused in ST and IO, so I can write
"sort a given array"; but I cannot write "increment a given integer
variable".

Of course, you can tell me to use mutable arrays of length 1 to
simulate mutable variables.  Fine!