tutorials-haskell

Haskell tutorial

by Bayle Shanks

some code samples are from __Yet Another Haskell Tutorial__ which is by Hal Daume III

I'm writing this after I read some tutorials myself but before actually programming anything in Haskell -- it's a "blind leading the blind" situation. So this tutorial is probably filled with errors and misunderstandings. After I get some experience with Haskell I might come back and check it over and then remove this warning, if I ever get around to it

Preface

This (currently unfinished) tutorial aims to be short yet to cover topics through basic monads.

Much of the bulk of the text consists of short code examples and interpreter traces.

The intended reader has programming experience, but only in imperative languages, and wants a quick introductory tour of Haskell. The tutorial is not intended to be difficult, however in the interest of brevity many things are demonstrated without much explanation.

It does not aim to be comprehensive, so after reading it, interested parties may want to read a longer tutorial to get the details that are skipped here. Hopefully, after reading this tutorial, you will be able to breeze through a longer one.

Copyright

Copyright 2008 Bayle Shanks. You may copy this document under the terms of either:

whichever you prefer (but not in any other circumstances without prior permission).

Note to people reading this on the wiki

(ignore this if you are reading the PDF). I'm using EasyLatex? to compile the wiki source of this page into a PDF. So don't mind the occasional LaTeX? commands. For example:

\usepackage{times} \usepackage[margin=3.7cm]{geometry}

\newpage


Hello World


As of this writing, the most popular implementation of Haskell seems to be ghc, the Glasgow Haskell Compiler. The GHC project also produces ghci, an interpreter. In this tutorial, we'll use ghci (although of course most of the things that we'll talk about are general features of the language that will be in any implementation). So go install ghci now.

When you start ghci, you get an intro banner and then a prompt:

Prelude> 

To quit, type Cntl-D.

Start ghci again. You can enter Haskell directly in the interpreter, for example:

Prelude> print "Hello World"
"Hello World"
Prelude> 

If you want to put this in a file, here's how. Each file is a module. The module name should be the same as the file name, except that the file name should have an .hs at the end and the module name should not.

So, create a file called Test.hs and put this into it:

module Test where
helloWorldFunction = print "Hello world"

To load a module in ghci, you say ":l modulename". So, to load module Test and then evaluate helloWorldFunction, you do:

Prelude> :l Test
[1 of 1] Compiling Test             ( Test.hs, interpreted )
Ok, modules loaded: Test.
*Test> helloWorldFunction
"Hello world"
*Test> 

Note that prompt changes from "Prelude" to "*Test". If you later change the sourcecode file and want to reload it, you can use :r, which reloads the current module.

In the previous example, the reason that there are quotes around "Hello world" is that the "print" command prints a representation of a value that is useful for debugging. If you just have a string that you want to send to the console directly, you use putStrLn. For example:

Prelude> print "Hello world"
"Hello world"
Prelude> putStrLn "Hello world"
Hello world
Prelude> 

If you want to compile a standalone executable, you need to make a Main module. Put the following into Main.hs:

module Main where
main = putStrLn "Hello world"

Now, at your operating system's command line, use ghc to compile:

$ ghc --make Main.hs -o main
$ ./main
Hello world

\newpage


Basic syntax


Haskell is **case-sensitive**

In addition,

  1. The names of types, typeclasses, and modules must begin with an upper-case letter.
  2. The names of infix functions must be composed of punctuation symbols.
  3. The names of almost1 everything else must begin with a lower-case letter.

Types

In Haskell, every expression has a type2