Fast Mutable arrays and a general question about IO

Ron de Bruijn
Thu, 1 May 2003 11:48:15 -0700 (PDT)

Hi there,

I have been busy for a couple of days now, to just get
a bit more feeling on Haskell and I have read a bunch
of tutorials, also about Monads and experimented of
course. And now I want to use it for my real world
problem. I think I understand the languageprinciples
good enough, but I yet don't understand the syntax of
all of the functioninterfaces. And that's my problem. 

I want to create the fastest array as possible in
Haskell and I understood that I should use this
function(the function below that creates a mutable
array). The only problem is that I don't kwow how to
use it.

Constructing mutable arrays 
newArray :: (MArray a e m, Ix i) => (i, i) -> e -> m
(a i e) 
Builds a new array, with every element initialised to
Reading and writing mutable arrays 
readArray :: (MArray a e m, Ix i) => a i e -> i -> m e

Read an element from a mutable array 
writeArray :: (MArray a e m, Ix i) => a i e -> i -> e
-> m () 
Write an element in a mutable array 

I see it takes two arguments: a tuple of an index
type(I use Int, so for example (0,1)
0 for the lower bound, 1 for the upper bound.)
, but the e (according to "GHC documentation" it says
the "element type"). Well the type of my elements
should be my own datatype myDataType, but I can't even
get it done to create simple mutable array's of Int or

How can I make it work with my own datatype
myDataType(could you give some examples for the use of
the below functions?)?
Is it OK to say that in Haskell any value that is put
from an extern source of the program(reading a file,
reading stdin etc.) to a really pure function, must be
in a do notation(essentially being a Monadic

So to put it more concrete:
In any Haskell program that does IO there is always
some function that has this form:

do x<-someSource

So it's not possible to write some function that does
the following:
f do x<-getChar   --suppose the Char is a number 
      someSpecialReturnFunction x
and than the result of the IO is only Int after some
alterations of above code. 

P.S. Will it give any significant loss in speed if
myDataType is lazy (no use of the !-sign in the

P.P.S. If there are array's that are even faster in
Haskell, please let me know?

Thanks in advance,

Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo.