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:
main :: IO ()
main = putStrLn "Hello, world!"Hello, world!
Then compile and run it from a terminal:
$ 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:
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
mainsays so, which is why the examples in this course useputStrLn.
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:
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:
$ 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:
executable greeter
main-is: Main.hs
hs-source-dirs: app
build-depends: base ^>=4.21, containers
default-language: GHC2021To 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?
- Cabal
- GHCi
- Hackage
- GHCup
What does :type not show in GHCi?
- The source code of
not not :: Bool -> BoolTrue- The file where
notis defined
What is the difference between print "hi" and putStrLn "hi"?
- None
printshows the value with quotes usingshow;putStrLnwrites the raw textputStrLnadds quotesprintonly works for numbers