Reading group: Learn You a Haskell: Part Five

Next week, on Friday, March 14 (Friday Piday!), I’d like to discuss the next two chapters:

  • Chapter 9: More Input and More Output
  • Chapter 10: Functionally Solving Problems

Chapter 9 builds on some of the I/O concepts we’ve learned so far, and chapter 10 has some general exercises on how to solve problems using functional programming instead of imperative programming.

After this next section, we start getting into the really exciting stuff: Applicatives, Monoids, and Monads!

We didn’t end up covering chapter 10 last Friday. We learned:

  • Haskell I/O is lazy like everything else
  • New I/O control functions: forever, interact, bracket
  • New I/O actions: openFile, readFile, writeFile, withFile, appendFile
  • File handle functions: hGetContents, hGetLine, etc
  • Command line arguments: getArgs, getProgName
  • Random numbers: random, randomR, mkStdGen, getStdGen
  • Bytestrings

We mentioned that we’ll learn better ways of handling random numbers later with the State monad. We’ll also learn how to use I/O better. I gave the following example:

-- Here's an example from the book:
main = do
    withFile "girlfriend.txt" ReadMode (\handle -> do
        contents <- hGetContents handle
        putStr contents)

-- You can also write it like this:
main = withFile "girlfriend.txt" ReadMode $ putStr <=< hGetContents