stuarray

STUArray with polymorphic type

可紊 提交于 2019-12-01 16:50:57
I want to implement an algorithm using the ST monad and STUArray s, and I want it to be able to work with both Float and Double data. I'll demonstrate on a simpler example problem: calculating a memoized scanl (+) 0 (I know it can be solved without STUArray , just using as example). {-# LANGUAGE FlexibleContexts, ScopedTypeVariables #-} import Control.Monad import Control.Monad.ST import Data.Array.Unboxed import Data.Array.ST accumST :: forall a. (IArray UArray a, Num a) => [a] -> Int -> a accumST vals = (!) . runSTUArray $ do arr <- newArray (0, length vals) 0 :: ST s (STUArray s Int a) forM

STUArray with polymorphic type

六眼飞鱼酱① 提交于 2019-12-01 16:06:20
问题 I want to implement an algorithm using the ST monad and STUArray s, and I want it to be able to work with both Float and Double data. I'll demonstrate on a simpler example problem: calculating a memoized scanl (+) 0 (I know it can be solved without STUArray , just using as example). {-# LANGUAGE FlexibleContexts, ScopedTypeVariables #-} import Control.Monad import Control.Monad.ST import Data.Array.Unboxed import Data.Array.ST accumST :: forall a. (IArray UArray a, Num a) => [a] -> Int -> a