Thanks, Tobias.<br><br>I guess my eyes kind of glazed over when I read "getElems :: (MArray a e m, Ix i) => a i e -> m [e]" in the docs, and didn't relate that to the meaning of "return"... lesson learnt.<div>
<br></div><div>About the extra type info needed -- what part of the type "ST s (STArray s Int Int)" is the compiler unable to infer?</div><div><br></div><div>I've worked out from this that the error message "no instance for"... might signal a missing type signature, but I'm having trouble working out the general lesson of when the compiler needs some extra hints.</div>
<div><br></div><div><br>On 25 May 2012 18:50, Matthew Moppett <<a href="mailto:matthewmoppett@gmail.com">matthewmoppett@gmail.com</a>> wrote:<br>> I've been trying to use mutable arrays in the ST monad, and wrote out a<br>
> little proof of concept function:<br>><br>> idST :: [Int] -> [Int]<br>> idST xs = runST $ do<br>> array <- newListArray (1, (length xs)) xs<br>> return (getElems array)<br>><br>> -- where idSt should be equivalent to id.<br>
><br>> And I get the error message:<br>><br>> Couldn't match type `[Int]' with `Int'<br>> In the return type of a call of `getElems'<br>> In the first argument of `return', namely `(getElems array)'<br>
> In a stmt of a 'do' block: return (getElems array)<br>><br>> Obviously I'm making a very simple mistake here, but I can't seem to spot<br>> it. Can anyone offer some advice?<br><br>'getElems array' already has type 'ST s [Int]', you don't need<br>
another 'return'. Furthermore you need to help out with the type<br>inference a little bit (it's similar to the read-show problem).<br>This should work:<br><br>idST :: [Int] -> [Int]<br>idST xs = runST $ do<br>
array <- newListArray (1, (length xs)) xs :: ST s (STArray s Int Int)<br> getElems array<br><br>(You could also replace STArray by STUArray.)<br></div>