Haskell Cheat Sheet
Haskell syntax, pure functions, algebraic data types, and lazy evaluation for statically typed functional programming.
2 PagesAdvancedApr 2, 2026
Basic Syntax
Bindings, control flow, and IO.
haskell
main :: IO ()main = do let age = 30 name = "Ada" pi' = 3.14159 if age >= 18 then putStrLn (name ++ " is an adult") else putStrLn (name ++ " is a minor") mapM_ (\i -> putStrLn ("Count: " ++ show i)) [0..4]
Functions & Pattern Matching
Defining functions by matching on structure.
haskell
-- function with type signaturesquare :: Int -> Intsquare x = x * x-- pattern matching on listsdescribe :: [Int] -> Stringdescribe [] = "empty"describe [x] = "one element: " ++ show xdescribe (x:xs) = "head " ++ show x ++ ", rest has " ++ show (length xs)factorial :: Int -> Intfactorial 0 = 1factorial n = n * factorial (n - 1)
Types & Type Classes
Algebraic data types and ad-hoc polymorphism.
haskell
data Shape = Circle Double | Rectangle Double Doublearea :: Shape -> Doublearea (Circle r) = pi * r * rarea (Rectangle w h) = w * hclass Describable a where describe :: a -> Stringinstance Describable Shape where describe (Circle r) = "Circle with radius " ++ show r describe (Rectangle w h) = "Rectangle " ++ show w ++ "x" ++ show h
Core Syntax & Types
Common Haskell notation.
- ::- reads as 'has type', used for type signatures
- ->- function type/arrow, e.g. Int -> Int
- data- defines a new algebraic data type
- Maybe a- Just value or Nothing, avoids null
- Either a b- Left error or Right success value
- ($)- low-precedence function application operator, avoids parens
- (.)- function composition operator
- IO a- type wrapping actions that perform side effects
Monads & do-Notation
Sequencing effectful computations with do blocks.
haskell
import Data.Maybe (fromMaybe)lookupUser :: Int -> Maybe StringlookupUser 1 = Just "Ada"lookupUser _ = Nothinggreet :: Int -> Maybe Stringgreet uid = do name <- lookupUser uid let msg = "Hello, " ++ name return msgmain :: IO ()main = do putStrLn (fromMaybe "unknown" (greet 1)) mapM_ print [1, 2, 3]
Defining Type Classes & Instances
Custom type classes with instance declarations.
haskell
class Describable a where describe :: a -> Stringdata Shape = Circle Double | Square Doubleinstance Describable Shape where describe (Circle r) = "circle r=" ++ show r describe (Square s) = "square s=" ++ show sarea :: Shape -> Doublearea (Circle r) = pi * r * rarea (Square s) = s * smain = putStrLn (describe (Circle 2))
Functor, Applicative & Monoid
Common abstractions over containers.
haskell
-- fmap maps inside a contextfmap (+1) (Just 3) -- Just 4(+1) <$> [1,2,3] -- [2,3,4]-- Applicative combines wrapped functions(+) <$> Just 2 <*> Just 5 -- Just 7-- Monoid combines values with <>mconcat ["a", "b", "c"] -- "abc"[1,2] <> [3,4] -- [1,2,3,4]-- Foldablefoldr (+) 0 [1,2,3] -- 6
Essential List Functions
Prelude functions for list processing.
- map / filter- transform or select elements of a list
- foldr / foldl'- reduce a list; foldl' is strict left fold
- zip / zipWith- combine two lists elementwise
- takeWhile / dropWhile- prefix elements while a predicate holds
- concatMap f- map then concatenate resulting lists
- iterate f x- infinite list x, f x, f (f x), ...
- span p- split into (takeWhile, dropWhile) tuple
Pro Tip
Haskell is lazily evaluated by default — use `seq` or strict data types (via `!` bang patterns) when accumulating large sums to avoid building up unevaluated thunks that blow the stack.
Was this cheat sheet helpful?
Explore Topics
#Haskell#HaskellCheatSheet#Programming#Advanced#BasicSyntax#FunctionsPatternMatching#TypesTypeClasses#CoreSyntaxTypes#Functions#CheatSheet#SkillVeris