Thinking Functional

@mkunikow: It depends a little on your definition of ‘functional purity’. There are some implementations, most notably Nock/Urbit comes to mind, that simply says ‘as far as code on our platform is concerned, there is nothing outside of the platform. Therefore, all function calls can be considered pure, and data structures can be made persistent.’

Other languages, like Haskell, that are basically syntactic sugar around the mathematical lambda calculus (which is pure by definition) preserve purity when dealing with side-effects by returning an object that represents a changed state of the world (It’s basically a reminder stating ‘Look! This was the second time I did a change!’). These values, which are of type IO in Haskell, can be used to chain multiple side-effectful operations together in a fixed order. This is necessary because there otherwise is no way to know in what order the functions are called, as this happens lazily. (more information on the Haskell wiki)

Can this be considered pure? Most people think yes, but of course this is subjective.

It is an interesting fact that the operations used for ‘passing the IO baton’ around together form a Monad, but you do not need to know how monads work in order to use side effects in Haskell.


If people want I can write a little introduction to how Monads work. Of course, it is very easy to fall for the ‘Monads are like Tacos’ fallacy: It is a very abstract concept, and an explanation that worked for me might not work for you.

Nevertheless, I think I can make a stab at it, using some great analogies with Fruit Salads and cooking that have their origins in the fun_land package. :slight_smile: