Can't make sense of newArray# docs

Antoine Latter aslatter at gmail.com
Tue Jan 18 23:42:50 CET 2011


On Tue, Jan 18, 2011 at 4:18 PM, Johan Tibell <johan.tibell at gmail.com> wrote:
> Hi,
>
> The docs for newArray# states:
>
> "Create a new mutable array of specified size (in bytes), in the
> specified state thread, with each element containing the specified
> initial value."
>
> Why is the size in bytes? Is Array# meant to be used both for boxed
> and unboxed values? For arrays of boxed values I'd expect the size to
> be the number of elements and the allocated size the number of
> elements multiplied by the size of a pointer. Are the docs correct? If
> so, how do I allocate an Array# of pointers? I'm trying to implement
> the following array type:
>
>    data MurableArray s a = Array { unArray :: !(MutableArray# s a) }
>
> i.e. an array of boxed values. I can't figure out how to write
>
>    new :: Int -> a -> ST s (MutableArray s a)
>
> where the first argument is the number of elements (of type a).
>
> Johan

The docs are wrong.

taking a look in GHC.Arr we find:

unsafeArray' :: Ix i => (i,i) -> Int -> [(Int, e)] -> Array i e
unsafeArray' (l,u) n@(I# n#) ies = runST (ST $ \s1# ->
    case newArray# n# arrEleBottom s1# of
        (# s2#, marr# #) ->
            foldr (fill marr#) (done l u n marr#) ies s2#)

The 'n' argument gets passed in from 'safeRange', so it is really the
number of elements you'd like to have in the created array.

Antoine

>
> _______________________________________________
> Glasgow-haskell-users mailing list
> Glasgow-haskell-users at haskell.org
> http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
>



More information about the Glasgow-haskell-users mailing list