[Newbie] Programming with MArray

Jay Cox sqrtofone@yahoo.com
Sat, 9 Feb 2002 18:08:15 -0600 (CST)

> Date: Fri, 8 Feb 2002 18:35:14 -0200
> From: =?iso-8859-1?Q?Jos=E9_Romildo_Malaquias?= <romildo@uber.com.br>
> To: haskell@haskell.org
> Subject: [Newbie] Programming with MArray
> --82I3+IH0IqGh5yIs
> Content-Type: text/plain; charset=iso-8859-1
> Content-Disposition: inline
> Content-Transfer-Encoding: 8bit
> Hello.
> To learn how to program with muttable arrays in Haskell, I have done
> a very simple program to sum two arrays. I am submitting it to this
> group so that it can be reviewd and commented. I have not find
> examples on how to program with muttable arrays.
> I would like for instance to see comments on the way the
> iteration over the array indices was done: using a list
> of indices. I looked for a way of incrementting an
> index, starting from the lower bound towards the upper
> bound, but failed in finding it. Is there other ways of
> iterating over the muttable array other then using
> its list of indices?
> Regards,
> Romildo

in short, I dont see why you couldn't have some helper function defined in
your "where" clause like:

update' i = if (i > some_max_bound) then return ()
            else do x1<- readArray v1 i
                    x2<- readArray v2 i
                    writeArray v3 (x1+x2)
                    update' (succ i)

I do know of papers where they write monadic functions this way so I
presume it is efficient.

indeed you might consider using the Bounded class for this purpose

--you can do this right??
newtype Foo = F Int deriving (Eq,Enum,Ord,...)

instance Bounded Foo where
  maxBound = F 9
  minBound = F 1

In terms of arbitrary indices? If I've read the current Haskell reports
right then if your type "a" doesnt have "Enum a" and (Eq "a" or Ord "a")
instance, it looks like iterating over a list is the only solution.

Jay Cox