5 ms·
Haskell lets you do side effects. The difference is that the type system carefully controls them.
by mightybyte 11y ago
Haskell lets you do side effects. The difference is that the type system carefully controls them.
- seanwilson 11y agoI was more referring to how OCaml allows mutable references and arrays, and allows IO similar to imperative languages whereas Haskell isn't so generous.
- wyager 11y agoHaskell actually does allow mutability in pure code using the ST monad. It's pretty cool; you can use ST to have mutable variables and arrays and such, but the mutable values aren't allowed to escape the ST "context". So you're still guaranteed immutability outside of the ST monad, because the type system locks all mutable values inside the monad. This is mostly only used in very-high-performance libraries that use algorithms based on array access.
- mightybyte 11y agoHaskell absolutely allows mutable references and arrays. See IORef [1] and MVector [2]. Notice how the functions that perform mutation are monadic. That's Haskell controlling the side effects and making sure that they don't pollute pure code. The ST monad stuff [3] that wyager mentioned is even cooler because it allows you to bundle up side effecting stuff and actually use it in pure code in a completely safe way, which is something that you can't do (without the dubious usafePerformIO) with the IO monad. [1] https://downloads.haskell.org/~ghc/latest/docs/html/libraries/base-4.8.1.0/Data-IORef.html https://downloads.haskell.org/~ghc/latest/docs/html/librarie... [2] http://hackage.haskell.org/package/vector-0.11.0.0/docs/Data-Vector-Mutable.html#g:10 http://hackage.haskell.org/package/vector-0.11.0.0/docs/Data... [3] https://downloads.haskell.org/~ghc/latest/docs/html/libraries/base-4.8.1.0/Control-Monad-ST.html https://downloads.haskell.org/~ghc/latest/docs/html/librarie...
- cygx 11y agoIt's a matter of definition. It's perfectly consistent to say Haskell does not allow side-effects as they stop being such once they have been reified.
- tome 11y agoBut then it's equally consistent to say that C doesn't allow side-effects because the whole program is reified.