Skip to content
AgileLogics

Haskell Cheat Sheet

Learn Haskell from the basics →

GHCi

ghci
Start the interactive interpreter
:t expr
Show the type of an expression
:i name
Info about a function, type, or class (and its instances)
:l Main.hs / :r
Load a file / reload after editing
:q
Quit GHCi
runghc main.hs
Run a file without compiling it first

Basics & Types

x :: Int
Type signature (Int, Integer, Double, Bool, Char, String)
let y = 5 in y * 2
Local binding in an expression
7 `div` 2 / 7 `mod` 2 / 7 / 2
Integer division / remainder / fractional division
fromIntegral n * 1.5
Convert an integral value to any numeric type
show 42 / read "42" :: Int
Value to String / String to value
/= && || not
Not equal, and, or, not

Functions

add :: Int -> Int -> Int
Curried function type: takes Int, returns Int -> Int
add x y = x + y
Function definition
\x -> x * 2
Lambda (anonymous function)
(+ 1) / (10 -) / (`div` 2)
Operator sections (partial application)
f . g / f $ x
Composition / low-precedence application
where / let ... in
Local definitions after or inside an expression

Lists

[1, 2, 3] / 1 : [2, 3] / [1 .. 10]
List literal / cons / range
head / tail / length / reverse / xs !! 2
Common list functions
map f xs / filter p xs
Transform / keep matching elements
foldr (+) 0 xs / foldl' (+) 0 xs
Fold right / strict fold left (Data.List)
[x * 2 | x <- xs, even x]
List comprehension
zip xs ys / take 3 xs / takeWhile p xs
Pair up / prefix / prefix while true

Pattern Matching

len [] = 0; len (_:xs) = 1 + len xs
Match on constructors, _ ignores a value
case m of Just x -> x; Nothing -> 0
case expression
f n | n < 0 = "neg" | otherwise = "ok"
Guards
all@(x:_)
As-pattern: name the whole value and its parts
let (a, b) = pair
Destructure a tuple

Data Types & Classes

data Shape = Circle Double | Rect Double Double
Algebraic data type (sum of products)
data User = User { name :: String, age :: Int }
Record with field accessors
u { age = 37 }
Record update (returns a new value)
newtype Email = Email String
Zero-cost wrapper type
deriving (Show, Eq, Ord)
Auto-generate class instances
instance Show Shape where show = ...
Hand-written instance

Maybe, Either, Functor, Monad

Just 3 / Nothing
Maybe: a value that might be missing
Right 3 / Left "error"
Either: a result or an error
fmap (+1) (Just 3) / (+1) <$> Just 3
Apply a function inside a Functor
(+) <$> Just 1 <*> Just 2
Applicative: combine wrapped values
m >>= f / pure x
Monad bind / wrap a plain value
maybe 0 (+1) m / fromMaybe 0 m
Unwrap a Maybe with a default

IO & do-notation

main :: IO ()
Program entry point
do { line <- getLine; putStrLn line }
Sequence IO actions, bind results with <-
putStrLn / print / getLine
Print a String / print with show / read a line
readFile / writeFile / appendFile
Whole-file I/O
mapM_ print xs / forM_ xs print
Run an action for each element
getArgs (System.Environment)
Command-line arguments

Tooling

ghcup install ghc / ghcup tui
Install and switch toolchain versions
cabal init / cabal build / cabal run
Create, build, and run a cabal project
cabal test / cabal repl
Run the test suite / GHCi with the project loaded
stack new app / stack build
Stack equivalents with curated snapshots
ghc -O2 Main.hs
Compile an optimized native executable
hlint . / ormolu -i Main.hs
Lint suggestions / auto-format