Last Updated: November 21, 2025
Haskell
Pure functional programming language
Basic Syntax
-- Function definition
add :: Int -> Int -> Int
add x y = x + y
-- List operations
numbers = [1, 2, 3, 4, 5]
doubled = map (*2) numbers
evens = filter even numbers
-- Pattern matching
factorial :: Int -> Int
factorial 0 = 1
factorial n = n * factorial (n - 1)
-- List comprehension
squares = [x^2 | x <- [1..10]]
-- Higher-order functions
applyTwice :: (a -> a) -> a -> a
applyTwice f x = f (f x)
Type System
| Item | Description |
|---|---|
Int
|
Fixed-precision integer |
Integer
|
Arbitrary-precision integer |
Float, Double
|
Floating-point numbers |
Bool
|
True or False |
[a]
|
List of type a |
(a, b)
|
Tuple of types a and b |
Maybe a
|
Optional value |
Monads
-- Maybe monad
safeDiv :: Double -> Double -> Maybe Double
safeDiv _ 0 = Nothing
safeDiv x y = Just (x / y)
result = do
a <- safeDiv 10 2
b <- safeDiv a 2
return (b + 1)
-- IO monad
main :: IO ()
main = do
putStrLn "What is your name?"
name <- getLine
putStrLn ("Hello, " ++ name)
Best Practices
- Everything is immutable by default
- Use pattern matching extensively
- Leverage lazy evaluation
- Understand monads for IO and state
💡 Pro Tips
Quick Reference
Haskell has no side effects in pure functions