Recently I have decided to teach myself the Haskell programming langauge. My first step was to grab a copy of the O’Reilly book on the langauge, which while slightly out of date, I can highly recommend. After digesting the earlier chapters, I was ready to write some simple programs. The first being the obligatory “Hello World!” program, which is incredibly simple.

1
2
3
4
module Main where

main :: IO ()
main = putStrLn "Hello World!"

The first line says that this is the Main module of the program being compiled. The next line main :: IO () declares a main function which performs IO operations and returns nothing, shown by the symbol (). The function main in the module Main is where the program execution begins.

These first two lines are actually omissable as the compiler assumes that unless specified, we are in the Main module and it can guess the types of functions from their implementations.

The final line specifies what the main function actually does. It performs the action putStrLn (print a string and then a newline) on the string “Hello World!”.

To compile and run this program using GHC (Glasgow Haskell Compiler) simply put the code in a file called “hello.hs” and type the following at the command line:

$ ghc -o hello hello.hs
$ ./hello
Hello World!
$

The next thing I usually write in a new language is a program to calculate numbers in the Fibonacci sequence, for which I have an easily understood program and a much faster algorithm which I will write about next time.