[Haskell-cafe] generic sum for an UArr

Manlio Perillo manlio_perillo at libero.it
Wed Mar 11 18:13:02 EDT 2009


Hi.

I have an array (Data.Array.Vector.UArr) of Word8 elements.
Elements are stored as Word8 to keep memory usage down.

Now I need to compute the sum of all the array elements; what is the 
best method?
The sum must be of type Int.


How efficient is to simply convert to an UArr Int, using:

   mapU fromIntegral v :: UArr Int
?


What about, instead, of something like:

   genericSumU :: (UA e, Integral e) => UArr e -> Int
   genericSumU = foldU add 0
       where
         add x y = x + fromIntegral y
?


Unfortunately the latter does not compile:

  Couldn't match expected type `Int' against inferred type `e'
       `e' is a rigid type variable bound by
           the type signature for `genericSumU' at 
bin/process-data-1.hs:64:19
     In the expression: foldU add 0
     In the definition of `genericSumU':
         genericSumU = foldU add 0
                     where
                         add x y = x + fromIntegral y


Moreover, this is not really a generic version.
I would like to have:

   genericSumU :: (Num i, UA e, Num e) => UArr e -> i

is this possible?



Thanks  Manlio


More information about the Haskell-Cafe mailing list