[Haskell-cafe] Newbie: Appending arrays?

Chaddaï Fouché chaddai.fouche at gmail.com
Fri Jul 11 08:58:00 EDT 2008


2008/7/11 Dmitri O.Kondratiev <dokondr at gmail.com>:
> I don't quite understand how Data.Array.Diff work.
> I tried this:
>
>> let arr = listArray (1,3) [1..3] :: DiffArray Int Int
>
> then:
>> replaceDiffArray arr [(1, 777)]
> array (1,3) [(1,1),(2,777),(3,3)]
> Why when replacing first element the second one changes?

replaceDiffArray is low-level, nobody should use it, use the normal
IArray interface instead.
(To answer your question, replaceDiffArray works with low level index,
not the Ix class, all array are indexed by 0, 1, .. for it, so 1 is
the index of the second element of the array)

> and also trying to add 4-th element results in:
> Prelude Data.Array.Diff> replaceDiffArray arr [(4, 444)]
> array (1,3) [(1,1),(2,2),(3,3)]
>
> It looks like replaceDiffArray can not be used to add new element to the end
> of array?

No, the size of a DiffArray can't be changed : DiffArray are just an
IArray instance with better performances for update than the classic
IArray instance (which copy the entire content on every update...).

There are some libraries that allows you to change the size of your
array, be aware though that this operation is very costly (in every
language).
http://hackage.haskell.org/cgi-bin/hackage-scripts/package/ArrayRef

-- 
Jedaï


More information about the Haskell-Cafe mailing list