Fast Mutable arrays and a general question about IO

Ron de Bruijn rondebruijn@yahoo.com
Fri, 2 May 2003 03:56:05 -0700 (PDT)


> Why?  It's not always necessary to go for maximum
> speed; do you have a
> reason for needing it in this case?

Almost any operation in my program works on array's
and before the program terminates, there have been an
awful lot of operations on it. If there is an other
kind of array that mutates and doesn't create a copy
of it when changing a value and isn't more than 1,5
times as slow as an array in C++ then that should also
be ok. The program would run in a scale of hours or
days. And if it's twice as fast, than I could
calculate two times as much. 

> > 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 undefined.
> 
> No; that's newArray_.  (newArray bounds x) builds an
> array with every
> element initialized to x.
> 
> > 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.),
> 
> I assume you're talking about newArray here, right? 
> If so, you're
> correct.
> 
> > 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 Bool.
 
> 
> I think you mean `above'.  In any case, could you
> show us some code you
> have that's not working?  That'll make it much
> easier for us to answer
> your questions.
> 

Well I don't have really code, because I want to begin
programming it. 

I will restate my goal: I want to be able to read,
update and create arrays in Haskell of type
myDataType(suppose my constructor function is Con Int
String Int on a way that's fast, preferabily the
fastest method. 

> > 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
> > operation)?
> 
> Yes.  More specifically, they don't need to use
> do-notation (although if
> you don't know why not, don't worry about it), but
> they do have to be
> monadic.  Specifically, they have to use the IO
> monad.

I know it's also possible to use (>>=) and a function
that works on values of the bound variable. 
> 
> > 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
> >   
>
putBoundVariableToSomeOtherFunctionThatDoesMonadicOperations
> >      (restOfPureFunctionalProgramForExample x)
> 
> No.

Why not? 

Greets Ron
 


__________________________________
Do you Yahoo!?
The New Yahoo! Search - Faster. Easier. Bingo.
http://search.yahoo.com