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
.
IO
is suitable for mutation - or rather, why mutation becomes pure when encapsulated inIO
. Historical/design suggests that there was also a choice in the matter, which I wasn't aware ofdata Op = Assign Int | Output
and aprogram = [Assign 1, Assign 2, Output, Assign 42, Output]
then you could undoubtedly create a function that takesprogram
and outputs[2,42]
. Does that meanAssign 2
has a side effect? No. Does it meanOutput
returns a different value every time? No. But still, if you create a list of them you can still somehow kinda-sorta program as if you have mutable state. Monads are basically fancy ways of creating (potentially infinite) sequences of such steps.