Installation, Tools and GHCi
2.1 Installing Haskell (GHCup)
# Linux/macOS/WSL
curl --proto '=https' --tlsv1.2 -sSf https://get-ghcup.haskell.org | sh
# Windows (PowerShell): see https://www.haskell.org/ghcup/ for the one-line installer
ghcup tui # interactive UI to install/select GHC, cabal, Stack, HLS
ghcup install ghc recommended && ghcup set ghc recommended
ghcup install cabal latest
ghcup install hls latest
ghc --version && cabal --version2.2 Tools
The compiler (ghc Main.hs) Interactive REPL (ghci) Build tool and package manager (Hackage)
Alternative build tool using curated Stackage snapshots Haskell Language Server (IDE support: types on hover, errors, refactoring)
Central package repository (hackage.haskell.org) Search functions by name or type signature (hoogle.haskell.org) Linter with refactoring suggestions Ormolu / Fourmolu / stylish-haskell Fast file-watching type checker
2.3 Hello, World
-- Main.hs
module Main where
main :: IO ()
main = do
putStrLn "Hello, World!"
let name = "Haskell"
putStrLn ("Hello, " ++ name ++ "!")runghc Main.hs # interpret and run
ghc -O2 Main.hs -o hello # compile to a native executable
./hello
cabal init # create a project (interactive)
cabal run # build and run the project2.4 Using GHCi
ghci> 2 + 3 * 4
14
ghci> :type map -- :t shows the type
map :: (a -> b) -> [a] -> [b]
ghci> :info Maybe -- :i shows definition and instances
ghci> :kind Either -- :k shows the kind
Either :: * -> * -> *
ghci> :load Main.hs -- :l loads a file; :r reloads
ghci> :browse Data.List -- list a module's exports
ghci> :doc foldr -- show documentation
ghci> :set +t -- show types after each evaluation
ghci> :set -XOverloadedStrings
ghci> let square x = x * x
ghci> square 7
49
ghci> :{
ghci| fact :: Integer -> Integer
ghci| fact 0 = 1
ghci| fact n = n * fact (n - 1)
ghci| :}
ghci> fact 25
15511210043330985984000000
ghci> :quit2.5 Comments and Layout
Haskell uses layout (indentation) to delimit blocks (like Python), or explicit { ; }. Indent consistently with spaces.
-- single-line comment
{- multi-line
{- nestable -} comment -}
-- | Haddock documentation for the next declaration
-- ^ Haddock documentation for the previous item