July 11, 2023

IO in Haskell

Here you have the biggest reason that Haskell isn't used for more things in the real world. IO is rocket science.

If you pick up the book "Learn you a Haskell ..." you are probably mildly surprised that it doesn't start off like every other programming book in the world with the traditional "Hello World" example. In fact it won't be until chapter 8 on page 154 that you see the "Hello World" example. There is a reason for this, but maybe not a good one. The book avoids dealing with IO directly by "suggesting" you use GHCI

Here is the "Hello World" example by the way:

#!/usr/bin/runghc
main = putStrLn "Hello World!"
It is really just one line of Haskell. Let's look at it just a little. This shows us how a Haskell program is set up. You define a function main and that function is what runs. Not greatly surprising, so what is the big deal.

The big deal is that "main" is invoked in a special way. In particular, main is an IO action. And IO action is a certain sort of monad and as such there are special things about it.

It is important not to get the business of being an IO action and being a Monad confused. As someone new to Haskell, you are likely to run into both of these concepts at more or less the same time. There are lots of different things that are Monads besides IO actions and Monads ought to be a topic all their own for another time.

The problem is that most treatments of Haskell IO pull in all kinds of Monad syntax and operators -- without telling you that is what they are. Such as "do" notation, which can be a controversial topic all its own.

Haskell IO functions


Feedback? Questions? Drop me a line!

Tom's Computer Info / [email protected]