Home > OS >  In Haskell, how to generate an arbitrary number of mutable variables and put them in a vector?
In Haskell, how to generate an arbitrary number of mutable variables and put them in a vector?

Time:11-24

Lets say I wanted to make 4 mutable arrays then later access them and mutate them based on their index of a vector :

 fn = do $
     v1 <- newMArray (Sz1 1) 0
     v2 <- newMArray (Sz1 1) 0
     v3 <- newMArray (Sz1 1) 0
     v4 <- newMArray (Sz1 1) 0
     let vls = Data.Vector.fromList [v1,v2,v3,v4]
     loopM 0 (<= 3) ( 1) (\k -> modifyM_ (vls ! k) ( 1) 0)
     
 
    

Now how would I generate N number of mutable arrays simply by supplying an integer N as an argument to fn?

CodePudding user response:

A generateM will get you where you need to go.

generateM 4 (\_ -> newMArray (Sz1 1) 0)
  • Related