February 17th, 2006

  • evan

haskell getting started

Two people have asked me about this recently, so:

One hurdle for learning Haskell is that it seems like all of the code you find online doesn't work when you type it into the interpreter.

This is because (unfortunately, though probably for good reasons that aren't apparent to me) Haskell programs are typically a collection of definitions and the REPL only evaluates expressions. So 2 * 2 works fine but x = 2 * 2 doesn't.

Instead, you have to make a file. I then use ghci foo.hs to load it into the REPL, where you can write main to run it or any other expression to test code. And to just run a file directly, use runhaskell foo.hs .

(PS: I lied in the above. It's actually that everything given at the prompt is interpreted within the IO monad. So you can do let x = 2 * 2 in the REPL, but this is a confusing way to get started, because the syntax is different than normal code.)