简体   繁体   中英

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

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 ?

A generateM will get you where you need to go.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM