Chapter 1: Getting Started
This chapter introduces the basics of Haskell, including type signatures, functions, and data structures. For example:
```haskell
mySum :: [Int] -> Int
mySum xs = sum xs
data Person = Person { name :: String, age :: Int }
```
Chapter 2: The Essence of Haskell
This chapter covers the core concepts of Haskell, such as laziness, immutability, and monads. For instance:
```haskell
fib :: Int -> Int
fib n = if n <= 1 then n else fib (n - 1) + fib (n - 2)
-- do notation for monads
do
x <- readLn :: IO Int
let y = x + 1
print y :: IO ()
```
Chapter 3: Types and Typechecking
This chapter delves into Haskell's powerful type system, including type classes, polymorphism, and type inference. Example:
```haskell
data Eq a => MyType a = MyType a
instance Eq Int where
(==) = undefined
instance Eq MyType where
(==) (MyType a) (MyType b) = a == b
```
Chapter 4: Functions
This chapter covers various aspects of functions in Haskell, including currying, function composition, and higher-order functions. For example:
```haskell
-- currying
myFunc :: Int -> Int -> Int
myFunc x y = x + y
-- function composition
(++) :: String -> String -> String
(.) :: (b -> c) -> (a -> b) -> (a -> c)
(++) . myFunc :: Int -> Int -> String
-- higher-order function
map :: (a -> b) -> [a] -> [b]
```
Chapter 5: Lazy Evaluation
This chapter explains lazy evaluation in Haskell and its implications. Example:
```haskell
-- Fibonacci sequence (lazy)
fibs :: [Int]
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
```
Chapter 6: Recursion
This chapter covers different types of recursion in Haskell, including structural recursion and guarded recursion. Example:
```haskell
-- structural recursion on lists
length :: [a] -> Int
length [] = 0
length (x:xs) = 1 + length xs
-- guarded recursion
isEven :: Int -> Bool
isEven n
| n == 0 = True
| otherwise = isOdd (n - 1)
```
Chapter 7: Data Structures
This chapter introduces the commonly used data structures in Haskell, such as lists, trees, and maps. Example:
```haskell
data Tree a = Leaf a | Node (Tree a) a (Tree a)
tree :: Tree Int
tree = Node
(Node (Leaf 1) 2 (Leaf 3))
4
(Node (Leaf 5) 6 (Leaf 7))
```