2 ms·
I appreciate this kind of distinction. However, I wouldn't be so quick to say that purely functional state transitions are unnatural or difficult. If you just f
by mbrock 12y ago
I appreciate this kind of distinction. However, I wouldn't be so quick to say that purely functional state transitions are unnatural or difficult. If you just formulate it slightly differently, it makes lots of sense: the point of the program is to calculate the next state given a current state and some input. And that's natural to formulate as a pure function of type (Input, State) → State.
Let's also not overestimate the elegance, naturalness, or correctness of the typical imperative approaches to control systems... Most programs are already difficult to understand, think about, predict, and change. And a huge reason is that they tend to lack principles. How often do you look at code and feel like there's no reason to believe that it works correctly, other than the fact that it seems to? And then you look at the growing bug count in the issue tracker...
State monads are (from one perspective) an implementation pattern used in Haskell to encapsulate state transitions. They're not necessary, and actually they're mostly used because they enable some syntactic sugar.
I've written very stateful code in Haskell, professionally, and it's been quite nice. No worse than any other language, and nicer in many ways. STM and light-weight threads go a long way.
I wish I had an example of a well-designed CRUD-ish system in Haskell to point to. Maybe there are some. I haven't had the opportunity to make one, but I feel confident that some very nice design patterns could emerge.
To give some indication that semi-nice stateful things can be written in Haskell in natural styles, here is a very simple component of an "IRC relay" system I never quite finished. [1] It's an independent process that reads IRC messages from a RabbitMQ queue, and writes "PONG" for every "PING". It's based around the `pipes` library and as you can see the basic effect of the program is explicitly stated in the main function.
[1]: https://github.com/mbrock/klatch/blob/master/Klatch/Ponger/Ponger.hs https://github.com/mbrock/klatch/blob/master/Klatch/Ponger/P...