Haskell Language Getting started


Example

Online REPL

The easiest way to get started writing Haskell is probably by going to the Haskell website or Try Haskell and use the online REPL (read-eval-print-loop) on the home page. The online REPL supports most basic functionality and even some IO. There is also a basic tutorial available which can be started by typing the command help. An ideal tool to start learning the basics of Haskell and try out some stuff.

GHC(i)

For programmers that are ready to engage a little bit more, there is GHCi, an interactive environment that comes with the Glorious/Glasgow Haskell Compiler. The GHC can be installed separately, but that is only a compiler. In order to be able to install new libraries, tools like Cabal and Stack must be installed as well. If you are running a Unix-like operating system, the easiest installation is to install Stack using:

curl -sSL https://get.haskellstack.org/ | sh

This installs GHC isolated from the rest of your system, so it is easy to remove. All commands must be preceded by stack though. Another simple approach is to install a Haskell Platform. The platform exists in two flavours:

  1. The minimal distribution contains only GHC (to compile) and Cabal/Stack (to install and build packages)
  2. The full distribution additionally contains tools for project development, profiling and coverage analysis. Also an additional set of widely-used packages is included.

These platforms can be installed by downloading an installer and following the instructions or by using your distribution's package manager (note that this version is not guaranteed to be up-to-date):

  • Ubuntu, Debian, Mint:

    sudo apt-get install haskell-platform
    
  • Fedora:

    sudo dnf install haskell-platform
    
  • Redhat:

    sudo yum install haskell-platform
    
  • Arch Linux:

    sudo pacman -S ghc cabal-install haskell-haddock-api \
                   haskell-haddock-library happy alex
    
  • Gentoo:

    sudo layman -a haskell
    sudo emerge haskell-platform
    
  • OSX with Homebrew:

    brew cask install haskell-platform
    
  • OSX with MacPorts:

    sudo port install haskell-platform
    

Once installed, it should be possible to start GHCi by invoking the ghci command anywhere in the terminal. If the installation went well, the console should look something like

me@notebook:~$ ghci
GHCi, version 6.12.1: http://www.haskell.org/ghc/  :? for help
Prelude> 

possibly with some more information on what libraries have been loaded before the Prelude>. Now, the console has become a Haskell REPL and you can execute Haskell code as with the online REPL. In order to quit this interactive environment, one can type :qor :quit. For more information on what commands are available in GHCi, type :? as indicated in the starting screen.

Because writing the same things again and again on a single line is not always that practically, it might be a good idea to write the Haskell code in files. These files normally have .hs for an extension and can be loaded into the REPL by using :l or :load.

As mentioned earlier, GHCi is a part of the GHC, which is actually a compiler. This compiler can be used to transform a .hs file with Haskell code into a running program. Because a .hs file can contain a lot of functions, a main function must be defined in the file. This will be the starting point for the program. The file test.hs can be compiled with the command

ghc test.hs

this will create object files and an executable if there were no errors and the main function was defined correctly.

More advanced tools

  1. It has already been mentioned earlier as package manager, but stack can be a useful tool for Haskell development in completely different ways. Once installed, it is capable of

    • installing (multiple versions of) GHC
    • project creation and scaffolding
    • dependency management
    • building and testing projects
    • benchmarking
  2. IHaskell is a haskell kernel for IPython and allows to combine (runnable) code with markdown and mathematical notation.