Monadic admixture and purity of Haskell. How do they fit together?

How does monad construction help maintain cleanliness (in Haskell) while doing unclean things? When, for example, you give print "Hello" , do you execute clean or unclean code? This is a very subtle detail, but it helps to better understand the idea of ​​cleanliness and impurity in functional languages.

+7
source share
2 answers

Expression

 print "Hello" 

really clean. Since it does not print anything, but rather creates something that, when executed, prints "Hello".

Here is an analogy:

The monk writes on a piece of paper:

 Go to a bordell and do filthy things with the prostitutes there. 

Can we blame a monk for adultery, just because he wrote instructions for engaging in adultery?

+17
source

A question in stackoverflow may answer your question: In what sense is pure IO Monad?

In short, the monad itself is clean , but it can issue impure instructions. To be a little more specific, monads can be seen as a series of compositional descriptions of computations. Some of these calculations may be dirty (i.e., have a side effect), but the description itself is completely clean and clean.

+5
source

All Articles