[Haskell-cafe] Examples of using STUArray + help with converting code

Udo Stenzel u.stenzel at web.de
Fri Oct 14 10:45:14 EDT 2005


joel reymont wrote:
> I don't understand the syntax needed to create a new double or float
> array with newArray from Data.Array.MArray. I also don't yet
> understand how to cast that double array to read ints from it.
> 
> doubleToInts d = runST (
>     do arr <- newDoubleArray (1,2)
>        writeDoubleArray arr 1 d
>        i1 <- readIntArray arr 1
>        i2 <- readIntArray arr 2
>        return (i1,i2))

Just use newArray, writeArray and readArray.  Something (writeArray is
probably the easiest) will need an explicit type signature to resolve
the overloading.  Also from the GHC docs:

castSTUArray :: STUArray s ix a -> ST s (STUArray s ix b)
  Casts an STUArray with one element type into one with a different
  element type. All the elements of the resulting array are undefined
  (unless you know what you're doing...).

Note the part in parentheses.  This is Haskell, not C after all.  A
clean solution may not be that far away.


Udo.
-- 
I underthand that people theem to like it better now.  
That it ith written in thee pluth pluth inthtead of lithp.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org//pipermail/haskell-cafe/attachments/20051014/4415d95f/attachment.bin


More information about the Haskell-Cafe mailing list