Do-expression expansion in Haskell

127 Views Asked by At

I have a fragment of code from the Get Programming with Haskell book which looks as follows:

listToSTUArray :: [Int] -> ST s (STUArray s Int Int)
listToSTUArray vals = do
    let end =  length vals - 1
    myArray <- newArray (0,end) 0
    forM_ [0 .. end] $ \i -> do
      let val = vals !! i
      writeArray myArray i val
    return myArray

In order to better understand the full process of transformations inside the ST s context here I decided to expand these lines of code into the expression using >>, >>= and return. So my attempt results in this code:

listToSTUArray vals = (\end -> (newArray (0,end) 0) (length vals - 1)) >>= 
                      (\myArray -> (forM_ [...] >> return myArray))

Is it the correct expansion? And what is the general recipe to expand do-notations with forM_ or similar actions since they do some computations but throws away the result in a context?

0

There are 0 best solutions below