Haskell/Basic Haskell

The Haskell Compiler edit

While there are various implementations of Haskell, for the purposes of this course we will only be using GHC, the Glasgow Haskell Compiler, and any references to compiling or running Haskell will assume a modern version of GHC (version 7.10 or 8).

Installing GHC edit

TODO: update this section and talk about Cabal and Stack.

Linux edit

If you're on Linux, you might have a package manager, where you can install GHC. If you're not that lucky you should try and build it from source. Get the source from the website: http://www.haskell.org/ghc/download.html

Using GHC edit

Now that GHC is installed, let's write a simple "Hello world"-program (We'll explain this in detail later):

 main = putStrLn "Hello, world!"

Put the above in a file called HelloWorld.hs

GHC edit

To run it with ghc type:

 $ ghc HelloWorld.hs

Now you should have a file called HelloWorld which you can run, and it should print:

 Hello, world!

great.

runghc edit

runghc is very much like ghc, but instead of making an executable file, it simply runs the program, without making any new files.

GHCi edit

With your installation of GHC you should also have a program called ghci, an interactive version of the Haskell compiler. Interactive interfaces to compilers are often called REPLs or read, eval, print, loops. When run, you should see output similar to:

$ ghci
GHCi, version 7.10.3: http://www.haskell.org/ghc/  :? for help
Prelude> 

Note: the Prelude is a collection of commonly used functions, like a standard library, that allow us to more easily write Haskell without having to remember to import the same libraries in every program. It is imported by default.

The line

Prelude> 

in GHCI tells us which modules are currently in scope. However we can change it to whatever we like with the set command. For example:

Prelude> :set prompt λ>
λ>

Note: all commands to GHCI are prefixed with a colon. As GHCI says when you start it, you can view these commands with :? or :help.

Users of the Vim text editor may find various commands in GHCI are equivalent, such as :! for running shell commands from inside of GHCI. For example:

λ>:!uname 
Linux

Finally it is worth noting that GHCI has a scrollable history (with the arrow keys) and tab completion.

Setting up and becoming familiar with GHCI edit

For the purposes of learning Haskell, we'll want to change some of GHCI's default settings. Start by turning on type signatures:

λ>:set +t

Now when we give GHCI input it will print out the type of the expression as well as what it evaluates to!

λ>"Hello, world!"
"Hello, world!"
it :: [Char]

The last value given to GHCI is bound to the name it, thus we can re-evaluate it by typing:

λ>it
"Hello, world!"
it :: [Char]

Finally, it's worth noting that type signatures can be checked without evaluating an expression (which is useful if GHCI doesn't know how to print that type of value (such as functions)). This can be done with :type or :t

λ>:t it
it :: [Char]

Type signatures edit

Okay, so now that we've got GHCI to tell us about the types of things, we need to learn what it's actually saying.

Note: GHC and GHCI can have intimidating error messages when you first start. Try to break down the error message into small pieces and follow the types.

The notation :: indicates a type signature and can be pronounced as is of type or is a. Thus the English equivalent to "Hello, World" :: String is "Hello, World" is a String. But wait, wasn't the type of "Hello, World" [Char]? Yes! Haskell uses type synonyms to try and keep things simple whenever possible. You can find out information about various Haskell terms with the :info or :i command. So when we investigate String we find out that it's the same as [Char]:

λ>:i String
type String = [Char] 	-- Defined in ‘GHC.Base’