A Taste of Haskell

Getting Started with Stack

Stack - The Haskell Tool Stack

  • Cross-platform tool for developing Haskell projects
  • Installs GHC (Glasgow Haskell Compiler)
  • Downloads and installs package dependencies
  • Isolated environments for each project
  • Commands for building, testing, benchmarking, etc.

Installing Stack

On Linux, MacOs, run from a terminal


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

or


wget -qO- https://get.haskellstack.org/ | sh
          

On Windows, download and run installer

Full instructions on https://docs.haskellstack.org

Hello World!

Type the code in a file helloworld.hs


main = putStrLn "Hello, Haskell world!"
          

Compile and execute it with


> stack ghc helloworld.hs
[1 of 1] Compiling Main         ( helloworld.hs, helloworld.o )
Linking helloworld ...

> ./helloworld
Hello, Haskell world!
          

Interactive Haskell

Start an interactive session (REPL)


> stack ghci
Configuring GHCi with the following packages:
GHCi, version 8.2.2: http://www.haskell.org/ghc/  :? for help
Loaded GHCi configuration from ...
Prelude>
          

Load your Haskell program and call its main function


Prelude> :load helloworld.hs
[1 of 1] Compiling Main         ( helloworld.hs, interpreted )
Ok, modules loaded: Main.

*Main> main
Hello, Haskell world!
          

Using GHCi

Create functions and evaluate Haskell expressions


Prelude> 2 + 3 * (4 ^ 5)
3074
Prelude> 1 < 2
True
Prelude> factorial 0 = 1; factorial n = n * factorial (n - 1)
Prelude> factorial 50
30414093201713378043612608166064768844377641568960512000000000000
          

Multiline Input


Prelude> :{
Prelude| sumList [] = 0
Prelude| sumList (x:xs) = x + sumList xs
Prelude| :}
Prelude> sumList []
0
Prelude> sumList [1,2,3]
6
Prelude> sumList [1..10000]
50005000
          

Basic GHCi Commands


:load <file> or :l <file> - load Haskell module
:reload or :r             - reload Haskell module
:cd <dir>                 - change directory
:! <cmd>                  - execute shell command
:help or :?               - list available commands
:quit or :q               - quit GHCi
          

Congratulations

You're ready to start!