5

I am working on an program that uses a large list of IORef's to a data type. Which is the more memory/processor-efficient way to do this:

[IORef Foo]

or

IORef [Foo]

Ignore the fact that I am using lists instead of vectors or arrays.

2
  • On the other hand, [IORef] lets you change the number of items, while IORef [] doesn't. Therefore, a suggestion: use IORef [IORef]. Commented Jan 15, 2014 at 12:45
  • 2
    Are you sure you don't want IOArray instead? Commented Jan 15, 2014 at 16:10

1 Answer 1

9

With [IORef Foo] you can update elements easily, with IORef [Foo], you can only update the whole list. Since you're likely wanting to efficiently update elements without copying, you want [IORef Foo]. Think, you want a list of mutable things, not a mutable list of immutable things.

As an example

import Data.IORef

makeTest :: IO [IORef Int]
makeTest = sequence [newIORef 0, newIORef 1, newIORef 2]

main = do
  test <- makeTest
  readIORef (test !! 1) >>= print
  modifyIORef (test !! 1) (+1) -- Doesn't copy list
  readIORef (test !! 1) >>= print
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.