4 ms·
It's worth noting that things like print and random are just as much side effects as mutability. To eliminate random is easy: realize that random generation is
by tel 12y ago
It's worth noting that things like print and random are just as much side effects as mutability. To eliminate random is easy: realize that random generation is a function from a hidden random generator state to a "random" value and a new random generator state. Now use state-minimization techniques.
To eliminate print is much harder.
- rubiquity 12y agoprint is IO so of course it is hard to eliminate side effects.
- coolsunglasses 12y agoYes, but you can uncouple the API talking about and generating IO actions from the performance of those IO actions if you have a pure, lazy language.
- tel 12y agoIO monads aren't the only way. If all you want is print it's even quite easy.
- sanderjd 12y agoPlease do tell or link to more, if you're willing!
- tel 12y agoPrint alone is easy as you can just have functions output both their normal output and a listing of whatever they printed. These listings then get concatenated together (this is technically "the writer monad", but you don't need to know as much). IO was originally handled in Haskell as `main :: [String] -> [String]`. More generally, we might think `main :: [Input] -> [Command]`. This is obviously a pure function. If the types Input and Command are a bit like Input = SawChar Char | Tick UTCTime | FileRecv String Command = PrintChar Char | RequestTime | RequestFile FilePath you can imagine how a more general effects framework could be done purely. In practice, monads are a lot simpler than this. In particular, it can be easy to get desynchronized from your Commands and Inputs. Other language have more explicit effect typing as well. Conor McBride's `Frank` comes to mind, but it's fairly complex.