Getting Started With Haskell

Getting Started With Haskell

- 4 mins

These are the steps to quickly get started with Haskell.

We’ll install the GHC (Glasgow Haskell Compiler), a build tool (Stack) and an editor with IDE for Haskell. The IDE support for Haskell is getting better, but several of the currently available editors still have features that are in their early stages. According to this overview IntelliJ/HaskForce and VSCode/Haskero are among others the two combinations that provide best support for Haskell. At the time of writing, HaskForce still does not have support for stepwise debugging (see this issue), so I will go with VSCode and Haskero. To keep this guide short, I won’t talk about code formatting and testing. See Stylish Haskell for code formatting and QuickCheck and HSpec for testing.

Prerequisites:

  • Homebrew package manager: https://brew.sh/. HomeBrew also works on Linux and on Windows 10 via WSL.
  • On MacOS, you also need to have the XCode command line tools installed. Run xcode-select --install to install if you dont already have it.

Unless otherwise noted, all commands are executed in a terminal.

Quick start guide

  1. Install Stack:

    brew install haskell-stack

    Stack will automatically download and setup a sandboxed GHC when you initialize a new Stack project. The advantage is that you get a compiler version that is appropriate for your project.

    Optional step:
    Alternatively, you can tell Stack to look for a suitable GHC in your system PATH. Stack can still download a sandboxed compiler, but only if your existing compiler is incompatible with your project. Here is how you set up a global GHC:

    a) First, install the Haskell compiler:

    brew install ghc

    b) Then tell Stack that we want to use a global GHC:

    stack config set system-ghc --global true

    From the Stack documentation:

    If you are using the —system-ghc flag or have configured system-ghc: true either in the project stack.yaml or the global ~/.stack/config.yaml, stack will use the first GHC that it finds on your PATH, falling back on its sandboxed installations only if the found GHC doesn’t comply with the various requirements (version, architecture) that your project needs. Source: https://docs.haskellstack.org/en/stable/faq/

  2. Continue to installing an IDE as described below.

**Installing and preparing the IDE**
  1. Install Visual Studio Code and Haskell extensions:

    brew cask install visual-studio-code

    Once installed, open Visual Studio Code and install the Haskero extension (Preferences -> Extensions). You must exit Visual Studio Code for the extension to take effect.

    Finally, install Intero (from the command line):

    stack install intero

    Note: Some extensions (such as Haskelly) also tell you to install stack-run - this is not necessary. See my additionally tips below to get the same functionality.

  2. Initialize a new template project:

    stack new hello-world

  3. Update the src/Lib.hs file to print out “Hello, world!” to the command line:

    code . opens Visual Studio Code in the current folder.

    Update the line someFunc = putStrLn "someFunc" to someFunc = putStrLn "Hello, world!"

  4. Build and run the hello-word application:

    stack build and then

    stack exec hello-world-exe

  5. You should now see “Hello, world!” printed on the command line.

**Additional tip:**

If you wanna avoid having to run stack build and stack exec ... every time, then create an alias function in your .bashrc file:

stackrun() {
  stack build
  echo "Executing $1"
  stack exec $1
}

Add the function to your .bashrc file: nano ~/.bashrc, save with Ctrl+O and Ctrl+X. Then source ~/.bashrc makes the function available in your terminal. You can now run the command stackrun hello-world-exe.