[Haskell-cafe] Streamed creation of STArray from list?

Jason Dagit dagit at codersbase.com
Thu Feb 11 12:23:05 EST 2010


Hello,

I was wondering if there is a trick for generating a new STArray from a list
in such a way that you do not have to hold both the list and array in
memory?

http://www.haskell.org/ghc/docs/latest/html/libraries/array-0.3.0.0/Data-Array-MArray.html

As far as I can tell, that is the interface for creating ST Arrays.  For
example, newListArray looks almost perfect:

*newListArray* ::
(MArray<http://www.haskell.org/ghc/docs/latest/html/libraries/array-0.3.0.0/Data-Array-MArray.html#t%3AMArray>a
e m,
Ix<http://www.haskell.org/ghc/docs/latest/html/libraries/base-4.2.0.0/Data-Ix.html#t%3AIx>i)
=> (i, i) -> [e] -> m (a i e)

If I know the length of the list, I might expect newListArray to have the
memory behavior I want.  In my case, the code calls (length xs) to calculate
the length of the list.  As I understand it, that will force the spine of
the list into memory.  Can this be avoided?  The only trick that comes into
mind is the one used by the C++ vector class where you dynamically resize
the array and copy the values to the new array.  That's potentially a lot of
copying, right?

Is some other array better suited for this, such as vector or uvector?

Thanks,
Jason
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20100211/273b25d5/attachment.html


More information about the Haskell-Cafe mailing list