Getting started with Haskell

We will write and run programs written in the Haskell programming language. For your convenience, we have already installed the ghc Haskell compiler in the class directory (cs191s) on ieng6. So, all you need to do in order to start using Haskell is to log in into your class/school account on ieng6, and prep cs191cs if necessary. (Alternatively, you can download and install Haskell on your own computer. Haskell is freely available and well supported on a variety of platforms, so it should be an easy install.) In the following, it is assumed you are logged in, and ready to enter commands at the ieng6 shell prompt ieng6$. Check that everything is working properly by issuing the following commands at the prompt:

ieng6$ prep cs191s
ieng6$ ghci
GHCi version 7.8.3 ...   
Prelude> 1+1
2

Congratulations, you just used Haskell to compute 1+1! Next, follow the examples at Starting Out to evaluate some more complex Haskell expressions. You can leave GHCi issuing the command :q or simply typing control-D.

For more complex examples, you will want to use a text editor to type your programs. Let's try that out. Use your favorite text editor to type the following (one line) program into a file hello.hs:

main = putStrLn "Hello, World!"  

You can run the program (from the command line shell) using the command

ieng6$ runhaskell hello.hs
Hello, World!

You can also compile your program into an executable and run it with the commands

ieng6$ ghc hello.hs
[1 of 1] Compiling Main ...   
ieng6$ hello  
Hello, World!  

or load it from within ghci with :l hello.hs, and then execute it by evaluating main a the ghci prompt.

In order to compile some programs, you may also need some additional packages, like the xml package which you can install on your computer with the commands cabal update, cabal install xml. All required packages are already installed on ieng6. Installing packages is only needed if you use your own computer.

Programming in Haskell

For a good tutorial introduction to the language we recommend "Learn you a Haskell for Great Good!". Going through the first few chapters should be more than enough: we will only use the most basic features of the language. If you want to dig deeper into haskell, you can find plenty of documentation and other resources at www.haskell.org.

If you are not familiar with any language in the haskell family, here are a few notes that may save you a few headaches. As a notational convention, we use this font when referring to haskell syntax, and use mathematical notation f(x2) to illustrate its meaning.