TExcelTAD
Home › Haskell › Haskell Get Started

Haskell Get Started

You can follow this whole course in the browser: every example is compiled with GHC on a remote server. When you want to write Haskell on your own computer, this lesson shows what to install and how the tools fit together.

The toolchain

Tool What it is
GHC The Glasgow Haskell Compiler. Turns .hs files into native executables
GHCi GHC's interactive REPL: type an expression, see its value and type
Cabal The build tool and package format: projects, dependencies, cabal run
Stack An alternative build tool that pins a curated snapshot of package versions
HLS Haskell Language Server, for editor support (errors as you type, hover types)
GHCup The installer that manages all of the above

The recommended way to install is GHCup from haskell.org/ghcup. It installs GHC, Cabal, Stack and HLS and lets you switch GHC versions later. On Windows it runs from PowerShell; on macOS and Linux it is a one-line shell command. In VS Code, add the official Haskell extension, which uses HLS.

Your first file

Save this as hello.hs:

haskell · try it yourself
main :: IO ()
main = putStrLn "Hello, world!"
Hello, world!

Then compile and run it from a terminal:

bash
$ ghc hello.hs
[1 of 2] Compiling Main             ( hello.hs, hello.o )
[2 of 2] Linking hello
$ ./hello
Hello, world!

For quick experiments you can skip the compile step with runghc hello.hs, which interprets the file directly.

Playing in GHCi

GHCi is where most Haskell programmers try things out. Start it with ghci and type expressions at the ghci> prompt; it evaluates and prints them. Commands that start with a colon talk to GHCi itself:

text
ghci> 2 + 3 * 4
14
ghci> reverse "stressed"
"desserts"
ghci> :type not
not :: Bool -> Bool
ghci> let square x = x * x
ghci> square 12
144
ghci> :load hello.hs
[1 of 2] Compiling Main             ( hello.hs, interpreted )
Ok, one module loaded.
ghci> main
Hello, world!
ghci> :quit
GHCi command Effect
:type expr (:t) Show the type of an expression
:info name (:i) Show where a name is defined and its instances
:load file.hs (:l) Load a source file
:reload (:r) Reload after editing the file
:quit (:q) Leave GHCi

Note: GHCi prints the value of any expression you type. A compiled program prints nothing unless main says so, which is why the examples in this course use print and putStrLn.

From GHCi to a program

Everything you type in GHCi also works inside main when you wrap it in print. This program reproduces the session above:

haskell · try it yourself
square :: Int -> Int
square x = x * x

main :: IO ()
main = do
  print (2 + 3 * 4)
  print (reverse "stressed")
  print (square 12)
14
"desserts"
144

Notice that print shows a string with quotes (it uses the same show that GHCi uses), while putStrLn writes the text itself.

Projects with Cabal

A real application has several modules and depends on libraries from Hackage, the Haskell package repository. Cabal manages that:

bash
$ mkdir greeter && cd greeter
$ cabal init --non-interactive     # creates greeter.cabal and app/Main.hs
$ cabal run                         # downloads dependencies, builds, runs
Hello, Haskell!

The generated greeter.cabal file lists the executable, its source directory and its dependencies:

text
executable greeter
    main-is:          Main.hs
    hs-source-dirs:   app
    build-depends:    base ^>=4.21, containers
    default-language: GHC2021

To use a library you add it to build-depends and import its modules in your code. Stack works the same way with a stack.yaml and package.yaml; pick either, both are widely used.

Tip: The packages that ship with GHC (base, containers, text, bytestring, mtl, transformers, array, …) are the ones this course uses. Everything you learn here works in a normal Cabal project unchanged.

Test yourself

Which tool gives you an interactive prompt to evaluate Haskell expressions?

  1. Cabal
  2. GHCi
  3. Hackage
  4. GHCup

What does :type not show in GHCi?

  1. The source code of not
  2. not :: Bool -> Bool
  3. True
  4. The file where not is defined

What is the difference between print "hi" and putStrLn "hi"?

  1. None
  2. print shows the value with quotes using show; putStrLn writes the raw text
  3. putStrLn adds quotes
  4. print only works for numbers