[Haskell-beginners] "pure" versus "impure" code

Christopher Done chrisdone at googlemail.com
Thu May 19 22:43:21 CEST 2011


On 19 May 2011 22:12, Costello, Roger L. <costello at mitre.org> wrote:
> Is there a web site that defines "pure" versus "impure" code?

Lots of pages:

http://en.wikipedia.org/wiki/Pure_function
http://en.wikipedia.org/wiki/Referential_transparency_(computer_science)
http://en.wikipedia.org/wiki/Idempotence#In_computing

> - Pure code is code that does no I/O
>
> - Impure code is code that does I/O
>
> Is that correct?

That's roughly right for a sufficiently greedy definition of "IO".

> Also, it is my understanding that good software design is to isolate/separate the impure code from the pure code. Is that correct?

That's the idea. Whether it's correct is up for debate, but that's
what Haskellers believe. Pure code is inherently easier to compose,
reason about and change. It makes sense to make most of your program
pure, especially important logic. Here's an example:

This is an IRC (chat) server. This module is impure:

https://github.com/chrisdone/hulk/blob/master/src/Hulk/Server.hs

This module is pure:

https://github.com/chrisdone/hulk/blob/master/src/Hulk/Client.hs

Think Pinky and the Brain. Pure code is the brain. Impure code is pinky.

> Does that principle apply to all programming languages, or just Haskell?

Haskellers apply this principle wherever they can.



More information about the Beginners mailing list