1

Is there a function can do what the function arrayToList do:

import Data.Array.ST
import Control.Monad.ST

genArray :: ST s [Int]
genArray = do
   a <- new Array (0, 99) 0 :: ST s (STArray s Int Int)
   writeArray a 0 1
   {- ... write something to the array ... -}
   return arrayToList(a)

If not, how to write one?

RnMss
  • 3,696
  • 3
  • 28
  • 37

2 Answers2

7

You don't need IO for this, constructing a list is a pure operation:

genArray :: [Int]
genArray = runST $ do
  a <- newArray (0, 99) 0 :: ST s (STArray s Int Int)
  writeArray a 0 1
  {- ... write something to the array ... -}
  getElems a
Sjoerd Visscher
  • 11,840
  • 2
  • 47
  • 59
1

Use stToIO and getElems:

genArray :: IO [Int]
genArray = stToIO $ do
    a <- newArray (0,99) 0 :: ST s (STArray s Int Int)
    writeArray a 0 1
    getElems a
Alfonso Villén
  • 373
  • 1
  • 9