⏳
Loading cheatsheet...
Type system, pattern matching, monads, typeclasses, lazy evaluation, functors, and functional programming.
-- ── Basic Types ──
-- Int (bounded), Integer (arbitrary precision)
-- Float, Double (floating point)
-- Bool (True, False)
-- Char (single character)
-- String (text)
-- Type annotations
x :: Int
x = 42
y :: Double
y = 3.14
name :: String
name = "Haskell"
-- ── Lists ──
nums = [1, 2, 3, 4, 5]
empty = []
range = [1..10]
evens = [2, 4..20] -- step by 2
chars = ['a'..'z']
-- List operations
head nums -- 1
tail nums -- [2, 3, 4, 5]
nums !! 2 -- 3 (0-based index)
length nums -- 5
null nums -- False
reverse nums -- [5, 4, 3, 2, 1]
take 3 nums -- [1, 2, 3]
drop 3 nums -- [4, 5]
sort [3, 1, 2] -- [1, 2, 3]
nub [1, 2, 2, 3] -- [1, 2, 3]
zip [1,2,3] "abc" -- [(1,'a'),(2,'b'),(3,'c')]
-- List comprehensions
squares = [x^2 | x <- [1..10]]
evens = [x | x <- [1..20], even x]
pairs = [(x,y) | x <- [1..3], y <- [1..3]]
sumSq = sum [x^2 | x <- [1..10], x `mod` 2 == 0]
-- ── Tuples ──
pair = (1, "hello", True)
fst pair -- 1
snd pair -- "hello"
third (_, _, z) = z -- True (pattern matching)
-- ── Pattern Matching ──
describe :: Int -> String
describe n
| n < 0 = "negative"
| n == 0 = "zero"
| n < 10 = "small positive"
| otherwise = "large positive"
-- Pattern matching on lists
describeList :: [a] -> String
describeList [] = "empty"
describeList [x] = "singleton"
describeList [x, y] = "pair"
describeList (_:xs) = "long: " ++ show (length xs)
-- Pattern matching on tuples
swap :: (a, b) -> (b, a)
swap (a, b) = (b, a)
-- Guards in pattern matching
classify :: Int -> String
classify n
| n < 0 = "negative"
| n `mod` 2 == 0 = "even"
| otherwise = "odd"| Class | Methods | Example Types |
|---|---|---|
| Eq | (==), (/=) | Int, Bool, Char, String |
| Ord | compare, (<), (>) | Int, Double, Char |
| Show | show | All basic types |
| Read | read | Int, Bool, Double |
| Enum | succ, pred, toEnum | Int, Bool, Char |
| Bounded | minBound, maxBound | Int, Char, Double |
| Num | (+), (-), (*), abs | Int, Double |
| Integral | div, mod | Int, Integer |
| Functor | fmap | Maybe, [], IO |
| Monad | (>>=), return | Maybe, [], IO |
| Function | Description |
|---|---|
| succ n, pred n | n+1, n-1 |
| even n, odd n | Is even/odd |
| div n m, mod n m | Integer division/modulo |
| null xs | Empty list? |
| concat, ++ | Concatenate lists |
| map f xs | Apply f to each element |
| filter p xs | Keep elements matching p |
| foldl f z xs | Left fold |
| sum, product, length | Aggregations |
| zipWith f xs ys | Zip with function |
Maybe for nullable values and Either for error handling.-- ── Maybe (nullable) ──
safeDivide :: Int -> Int -> Maybe Int
safeDivide _ 0 = Nothing
safeDivide a b = Just (a `div` b)
-- Using Maybe with do notation
compute :: Int -> Int -> Int -> Maybe Int
compute a b c = do
x <- safeDivide a b
y <- safeDivide x c
return (x + y)
-- Functor: fmap
fmap (+1) (Just 5) -- Just 6
fmap (+1) Nothing -- Nothing
-- Applicative: <*>
Just (+) <*> Just 3 <*> Just 5 -- Just 8
Nothing <*> Just 5 -- Nothing
-- ── Either (error handling) ──
data Either a b = Left a | Right b
safeDivide :: Int -> Int -> Either String Int
safeDivide _ 0 = Left "Division by zero"
safeDivide a b = Right (a `div` b)
-- ── IO Monad (side effects) ──
main :: IO ()
main = do
putStrLn "What is your name?"
name <- getLine
putStrLn ("Hello, " ++ name ++ "!")
-- ── List Monad ──
-- [1,2] >>= \x -> [x, x*10]
-- = [1,10,2,20]
-- ── Monad Typeclass ──
class Monad m where
return :: a -> m a
(>>=) :: m a -> (a -> m b) -> m b
-- ── Functors ──
class Functor f where
fmap :: (a -> b) -> f a -> f b
-- <$> is infix fmap
(+1) <$> [1,2,3] -- [2,3,4]
(*2) <$> (Just 5) -- Just 10
-- ── Applicative ──
class Functor f => Applicative f where
pure :: a -> f a
(<*>) :: f (a -> b) -> f a -> f b
-- ── Common Monad Operations ──
-- mapM, forM, sequence, mapM_
-- unless, when, guard| Type | Use Case |
|---|---|
| Maybe a | Optional values, nullable returns |
| Either e a | Error handling with error type e |
| IO a | Side effects (print, file, network) |
| [a] | Multiple results (lists) |
| State s a | Stateful computation |
| ST s a | Mutable state in pure code |
| Law | Expression |
|---|---|
| Left identity | return a >>= f === f a |
| Right identity | m >>= return === m |
| Associativity | (m >>= f) >>= g === m >>= (\x -> f x >>= g) |
-- ── Function Declaration ──
add :: Int -> Int -> Int
add a b = a + b
-- Lambda (anonymous function)
double = \x -> x * 2
add = \a b -> a + b
-- Sections (partial application)
add :: Int -> Int -> Int
add a b = a + b
increment = add 1 -- Int -> Int
-- increment 5 = 6
-- Function composition
(.)
(.) :: (b -> c) -> (a -> b) -> (a -> c)
f . g = \x -> f (g x)
($)
($) :: (a -> b) -> a -> b
f $ x = f x -- removes parentheses
-- ── Higher-Order Functions ──
map :: (a -> b) -> [a] -> [b]
map (*2) [1, 2, 3] -- [2, 4, 6]
filter :: (a -> Bool) -> [a] -> [a]
filter even [1..10] -- [2, 4, 6, 8, 10]
foldl :: (b -> a -> b) -> b -> [a] -> b
foldl (+) 0 [1, 2, 3, 4, 5] -- 15
foldl (acc x -> acc + x) 0 [1..5]
foldr :: (a -> b -> b) -> b -> [a] -> b
foldr (:) [] [1, 2, 3] -- [1, 2, 3]
scanl :: (b -> a -> b) -> b -> [a] -> [b]
scanl (+) 0 [1, 2, 3] -- [0, 1, 3, 6]
zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (+) [1,2,3] [10,20,30] -- [11, 22, 33]
-- ── Type Class Instances ──
data Color = Red | Green | Blue
instance Show Color where
show Red = "Red"
show Green = "Green"
show Blue = "Blue"
instance Eq Color where
Red == Red = True
Green == Green = True
Blue == Blue = True
_ == _ = False
-- ── Newtype ──
newtype UserId = UserId Int
unUserId :: UserId -> Int
unUserId (UserId n) = n
-- ── Type Synonym ──
type Name = String
type Age = Int
type Person = (Name, Age)# ── GHC (Glasgow Haskell Compiler) ──
ghc Main.hs -o main # compile
./main # run
# ── Stack (build tool) ──
stack new my-project # create new project
stack build # build project
stack test # run tests
stack ghci # REPL with project loaded
stack exec ghc -- --version
stack install hlint # install tool
# ── Cabal ──
cabal init # create cabal file
cabal build # build
cabal test # test
cabal run # run executable
# ── Project Structure ──
# my-project/
# app/
# Main.hs
# src/
# MyModule.hs
# test/
# MyModuleTest.hs
# package.yaml / my-project.cabal