Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.



print is IO so of course it is hard to eliminate side effects.


IO monads aren't the only way. If all you want is print it's even quite easy.


Please do tell or link to more, if you're willing!


Print 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.


Yes, 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.




Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: