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

minh thu noteed at gmail.com
Tue May 9 13:31:53 EDT 2006


thanks for your answers,
i guess i have to look at strictness and such 'optimizations', ...
mt

2006/5/9, Bulat Ziganshin <bulat.ziganshin at gmail.com>:
> 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