[Haskell-cafe] How can i safely change the value of specified key ?

Bulat Ziganshin bulat.ziganshin at gmail.com
Thu Oct 22 03:40:07 EDT 2009

Hello zaxis,

Thursday, October 22, 2009, 11:28:14 AM, you wrote:

> aaa <- newIORef ([]::[(Int,Int)])
> writeIORef aaa [(1,1),(2,2),(3,3)] 

> then if i want to change aaa to [(1,1),(2,222),(3,3)] , what's the best way
> ? 
> re-write aaa is not permitted.

it's the only way. in Haskell, you have *immutable* values. aaa is a
reference to immutable value. you can mutate reference so it will
point to another immutable value but you cannot change this value.

there are two ways to make aaa==[(1,1),(2,222),(3,3)]. first, you can
apply function to whole value:

value <- readIORef aaa
writeIORef aaa (f value)

second, you may create list of IORefs, tuple of IORefs and so:

a <- newIORef (1,1)
let aaa = [a,b,c]

now aaa is a immutable list of mutable IORefs. of course, you can
create IORef pointing to list of IORefs too

Best regards,
 Bulat                            mailto:Bulat.Ziganshin at gmail.com

More information about the Haskell-Cafe mailing list