Slides for ' Programming in Haskell'

The website of Hutton's introductory Programming in Haskell book, now includes eleven powerpoint presentations covering most of the chapters in the book. Each set of slides is intended to be used for a one hour lecture.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

meh, no STATE

I was excited to see an example of Hangman in these slides, as it's an example of multiple states: the word to guess, the guessed letters, and the state of the hangman (which I suppose can be derived). I sank when I saw that it was actually stateless, depending on a "diff" function that simply revealed all letters in a second string contained in the first string. Apparently the player is trusted to enter whatever string he likes.

I can get lazy evaluation, pattern matching, currying, composition, maps, folds, and even monads in general, but I simply can't understand how to do non-trivial state in Haskell without a great deal of hardship. Muddling through one piece of global state is tricky enough, but for more than one, the answer seems to be to throw everything into one big state structure and make it only one state, or some dizzying combination of monad transformers.

Imagine trying to write Scrabble or a MUD in Haskell if Hangman is such a trial!

You want IORefs, or the ST monad

Even with monolithic state, you can use projection and mutation functions to read and write parts of a larger structure. But Haskell does have other ways of doing state, if needed. IORefs and STRefs are references to mutable cells (you can also have mutable arrays). You can run code in the ST monad that uses STRefs, and exit back into "pure" code when you're done. The advantage is that you can have as many discrete STRefs as you think you need, rather than having to thread a single monolithic state value through the code.

It's Friday

Ok. I didn't look at the slides, but since it's Friday, I thought I'd try my hand at whipping up a Haskell hangman. I'm curious as to how much better it could be with mutable global state.

import System.IO

data EKG = Alive | Dead deriving Eq

main = do
        hSetEcho stdin False
        hSetBuffering stdin NoBuffering
        let word = "lambda the ultimate"
        putStrLn $ "The puzzle is: " ++ progress " " word
        health  String -> String -> IO EKG
hangman 0 _ _ = return Dead
hangman n so_far answer =
       do putStrLn $ "you have "++(show n)++" guesses left. Enter next guess"
          letter  q `elem` current) answer)
           then return Alive
           else if (letter `elem` answer)
                 then hangman n current answer
                 else hangman (n-1) so_far answer

progress so_far answer = map (\l -> if (l `elem` so_far) then l else '_') answer


Monads

The slides about functional parsers provide a nice introduction to monads, by the way, without talking about all the scary things (i.e., algebraic properties).