[Haskell] how to 'accumulate' values efficiently (time and space) ?

Bulat Ziganshin bulat.ziganshin at gmail.com
Tue May 9 02:27:25 EDT 2006


Hello minh,

Monday, May 8, 2006, 12:28:35 PM, you wrote:

> acc1 ints = foldr (+) 0 ints

foldl?

> ----------------------------------------------------------------------

> acc2 ints = execState (mapM add2 ints) 0

> add2 :: Int -> State Int ()
> add2 i = do
>      acc <- get
>      put (acc + i)

put $! (acc+i)

> ----------------------------------------------------------------------

> acc3 ints = runST (acc3' ints :: ST s Int)

> acc3' ints = do
>      accRef <- newSTRef 0
>      mapM (add3 accRef) ints
>      acc <- readSTRef accRef
>      return acc

> add3 accRef i = do
>      modifySTRef accRef (+ i)

use readref/writeref and $!

-- 
Best regards,
 Bulat                            mailto:Bulat.Ziganshin at gmail.com



More information about the Haskell mailing list