Reputation: 7877
Why are mutable data structures and other mutability represented using IO
in functional languages? I'm looking at e.g. Haskell's IORef
or Idris' IOArray
.
I don't think I mean this as a historical or design question. I don't quite understand why IO
is suitable for mutation - or rather, why mutation becomes pure when encapsulated in IO
.
Upvotes: 1
Views: 216
Reputation: 120751
You don't need to represent them with IO
. It's possible to do them in ST
instead. But you can obviously represent them with IO
, where any dirty side effect can be achieved. So if you're working in IO
anyway, it's easiest to just do the mutations there too, this way you don't need to put any worry on welding different monads together. If you're not already working in IO
, you should use ST
however.
Upvotes: 5