Haskell is unlike any other programming language. It's purely functional with a strong type system and lazy evaluation. In practical terms, this means that Haskell treats programs a lot like math, so you have to think more than you type. Along with being arguably the most interesting language, Haskell has the reputation of being one of the most challenging programming languages to learn. Learning Haskell doesn't have to be difficult, and this book can help!

*Learn Haskell* teaches you the Haskell language and functional programming concepts while you hack on interesting problems that you'll find challenging but never frustrating. This example-filled tutorial will take you from beginning concepts to tackling the tough topics, like Monads, easing you along the way with increasingly sophisticated examples. You'll find lots of crystal-clear illustrations, hands-on exercises that reinforce the concepts, and open-ended tasks that encourage you to hack and explore Haskell on your own. By the time you're done, you'll be able to use Haskell as a platform for having fun with code and possess a deep understanding of functional programming.

# Lesson 0 Getting Started with Haskell

##### 0.1 Welcome to Haskell

##### 0.1.1 The Haskell Platform

##### 0.1.1 Text Editors

##### 0.2 The Glasgow Haskell Compiler (GHC)

##### 0.2.1 Basic GHC Usage

##### 0.3 Interacting with Haskell - GHCi

##### 0.4 Writing and working with Haskell code

##### 0.4.1 Interactively writing Haskell programs

##### 0.5 Exercises

# Unit 1 Functional Programming

## 1. Lesson 1 Functions and Functional Programming

### 1.1. Functions

### 1.2. Functional Programming

### 1.3. The Value of Functional Programming in Practice

#### 1.3.1. Variables

#### 1.3.2. Variables that are Variable

### 1.4. Exercises

## 2. Lesson 2 Lambda Functions and Lexical Scope

### 2.1. Lambda Functions

### 2.2. Writing our own Where clause

### 2.3. From Lambda to Let: Making our own variable variables!

### 2.4. Practical Lambda Functions and Lexical Scope

### 2.5. Exercises

## 3. Lesson 3 First Class Functions

### 3.1. Functions as Arguments

#### 3.1.1. Lambda Functions as arguments

#### 3.1.2. Example - custom sorting

### 3.2. Returning Functions

### 3.3. Exercises

## 4. Lesson 4 Closures and Partial Application

### 4.1. Closures - Creating Functions with Functions

### 4.2. Example: Generating URLs for an API

#### 4.2.1. Partial Application: Making Closures Simple

### 4.3. Putting it all together

### 4.4. Exercises

## 5. Lesson 5 Lists

### 5.1. The anatomy of a list

### 5.2. Lists and Lazy Evaluation

### 5.3. Common Functions on Lists

### 5.4. Exercises

## 6. Lesson 6 Recursion and Pattern Matching

### 6.1. Recursion

#### 6.1.1. Rules for recursion

#### 6.1.2. Our First Recursive Function: Greatest Common Divisor

#### 6.1.3. Pattern Matching

### 6.2. Exercises

## 7. Lesson 7 Writing Recursive Functions

### 7.1. Review: Rules of Recursion

### 7.2. Recursion on Lists

### 7.3. Pathological Recursion: Ackerman Function and the Collatz Conjecture

### 7.4. Exercises

## 8. Lesson 8 Higher Order Functions

### 8.1. Higher Order Functions

### 8.2. Map

### 8.3. Abstracting away recursion with map

### 8.4. Filtering a list

### 8.5. Folding a list

### 8.6. Exercises

# Capstone - Functional Object Oriented Programming with Robots!

###### C1.1 An object with one property: A cup of coffee.

###### C1.1.1 Creating a constructor

###### C1.1.2 Adding accessors to our object

###### C1.2 A more complex object: Let’s build fighting robots!

###### C1.2.1 Sending messages between objects

###### C1.3 Why Stateless programming matters

###### C1.4 Types - Objects and so much more!

###### C1.5 Summary

###### C1.6 Exercises

# Unit 2 Introducing Types

## 1. Lesson 9 Type Basics

### 1.1. Types in Haskell

### 1.2. Function Types

#### 1.2.1. Functions for converting to and from Strings

#### 1.2.2. Functions with Multiple Arguments

#### 1.2.3. Types for First Class functions

### 1.3. Type Variables

### 1.4. Exercises

### 1.5. Spot Quiz Solutions

## 2. Lesson 10 Creating our own types

### 2.1. Type Synonyms

### 2.2. Creating new types

### 2.3. Record Syntax

### 2.4. Exercises

### 2.5. Answers to Spot Quizzes

## 3. Lesson 11 Type Classes

### 3.1. Further Exploring Types

### 3.2. Type classes

### 3.3. The Benefits of Type Classes

### 3.4. Defining a Type class

### 3.5. Common Type Classes

#### 3.5.1. The Ord and Eq Type classes

#### 3.5.2. Bounded

#### 3.5.3. Show

### 3.6. Deriving Type classes

### 3.7. Exercises

### 3.8. Answers to Spot Quizzes

## 4. Lesson 12 Using Type Classes

### 4.1. A type in need of classes

### 4.2. Implementing Show

### 4.3. Type Classes and Polymorphism

### 4.4. Default Implementation and Minimum complete definitions

### 4.5. Implementing Ord

### 4.6. To Derive or Not to Derive?

### 4.7. Type classes for more complex types

### 4.8. Type class roadmap

### 4.9. Exercises

### 4.10. Answers to Spot Quizzes

# Capstone Exercise: Secret Messages!

###### C2.1 Ciphers for Beginners: ROT13

###### C2.1.1 Implementing our own ROT Cipher

###### C2.1.2 The rotN algorithm

###### C2.1.3 Rot encoding a String

###### C2.1.4 The problem with decoded odd-sized alphabets

###### C2.2 XOR: the magic of cryptography!

###### C2.3 Representing values as Bits

###### C2.4 The one-time pad

###### C2.4.1 Implementing our one-time pad

###### C2.5 A Cipher Class

###### C2.6 Summary

###### C2.7 Extensions

# Unit 3 - Programming in Types

## 1. Lesson 13 Creating Types with "and" and "or"

### 1.1. Product Types - Combing types with 'and'

#### 1.1.1. The curse of product types: Hierarchical design.

### 1.2. Sum types: Combining Types with 'or'

### 1.3. Putting together our books store

### 1.4. Exercises

### 1.5. Answers to Spot Quizes

## 2. Lesson 14 Design by Composition : Semigroups and Monoids

### 2.1. Intro to composability - Combining Functions

### 2.2. Combining like Types: Semigroups

#### 2.2.1. The Color Semigroup

#### 2.2.2. Making Color associative and using Guards

### 2.3. Composing with Identity: Monoids

#### 2.3.1. mconcat: Combining multiple Monoids at once

#### 2.3.2. Monoid Laws

### 2.4. Practical Monoids - Building Probability Tables

### 2.5. Exercises

### 2.6. Answers to Spot Quizzes

## 3. Lesson 15 Parameterized Types

### 3.1. Types that take arguments

#### 3.1.1. A more useful parameterized type: Triple

#### 3.1.2. Lists

### 3.2. Types with more than one parameter

#### 3.2.1. Tuples

#### 3.2.2. Kinds: Types of Types

#### 3.2.3. Data.Map

### 3.3. Exercises

## 4. Lesson 16 The Maybe Type: Dealing with missing values

### 4.1. Introducing Maybe: Solving missing values with types

### 4.2. The problem with Null

#### 4.2.1. Handling missing values with errors

#### 4.2.2. Returning Null values

#### 4.2.3. Maybe as a novel solution to missing values

### 4.3. Computing with Maybe

### 4.4. Back to the Lab! More complex computation with Maybe

### 4.5. Exercises

### 4.6. Answers to Spot Quizzes

# Capstone: Time Series

###### C3.1 Our data and the TS data type

###### C3.1.1 Building a Basic Time Series Type

###### C3.2 Stitching together our TS data using Semigroup and Monoid

###### C3.2.1 Making TS an Instance of Monoid

###### C3.3 Performing calculations on our Time Series

###### C3.3.1 Calculating the min and max values for our time series

###### C3.4 Transforming Time Series

###### C3.4.1 Moving average

###### C.3.5 Summary

###### C.3.6 Extensions

# Unit 4 - IO in Haskell

## 1. Lesson 17 Hello World! - Introducing IO Types

### 1.1. IO Types - Dealing with an impure world

### 1.2. IO Actions are not functions

#### 1.2.1. Examples of IO Actions

#### 1.2.2. Keeping values in the context of IO

### 1.3. Do-notation

### 1.4. An example: Command line Pizza cost calculator

#### 1.4.1. A peak at Monads - Do notation in Maybe

### 1.5. Exercises

### 1.6. Answers to Spot Quizzes

## 2. Lesson 18 Interacting with the Command line and Lazy IO

### 2.1. Interacting with the command line the non-lazy way

### 2.2. Interacting with Lazy IO

#### 2.2.1. Thinking of our problem as a lazy list

### 2.3. Exercises

### 2.4. Answers to Spot Quizzes

## 3. Lesson 19 Working with Text and Unicode

### 3.1. The Text Type

#### 3.1.1. When to use Text vs String

### 3.2. Using Data.Text

#### 3.2.1. OverloadedStrings and Haskell language extensions

#### 3.2.2. Basic Text utilities

### 3.3. Text and Unicode

#### 3.3.1. Searching Sanskrit

### 3.4. Text IO

### 3.5. Answers to Spot Quizzes

## 4. Lesson 20 Working with Files

### 4.1. Opening and closing files

### 4.2. Simple IO tools

### 4.3. The Trouble with Lazy IO

### 4.4. Strict IO

#### 4.4.1. When to use Lazy vs Strict

### 4.5. Exercises

### 4.6. Answers to Spot Quizzes

## 5. Lesson 21 Working with Binary Data

### 5.1. Working with binary data using ByteString

### 5.2. Glitching JPEGS

#### 5.2.1. Inserting Random Bytes

#### 5.2.2. Sorting random bytes

#### 5.2.3. Chaining together IO Actions with foldM

### 5.3. ByteStrings, Char8 and Unicode

### 5.4. Exercises

### 5.5. Answers to Spot Quizzes

# Capstone: Processing Binary Files and Book Data

###### C4.1 Working with Book Data

###### C4.2 MARC Records

###### C4.2.1 The Structure of a MARC record

###### C4.2.2 Getting the data

###### C4.2.3 The Leader and Iterating through our records

###### C4.2.4 Reading the Directory

###### C4.2.5 Using the directory to look up fields

###### C4.2.6 Processing the directory entries and looking up MARCFields

###### C4.2.7 Getting Author and Title information from a Marc Field

###### C4.3 Putting it all together

###### C4.4 Summary

###### C4.5 Extending the Exercise

# Unit 5 Types Within A Context

## 1. Lesson 22 The Functor Type Class

### 1.1. An Example: Computing in a Maybe

### 1.2. The Functor Type Class

### 1.3. Functors are everywhere!

#### 1.3.1. One interface for Four very different Problems

#### 1.3.2. Converting a Maybe RobotPart to Maybe Html

#### 1.3.3. Converting a list of RobotParts to a List of Html

#### 1.3.4. Converting A Map of RobotParts to Html

#### 1.3.5. Transforming an IO RobotPart into IO Html

### 1.4. Exercises

### 1.5. Answers to Spot Quizzes

## 2. Lesson 23 A peek at the Applicative Type Class: Using Functions in a Context

### 2.1. A command line application for calculating the distance between

#### 2.1.1. The limitations of Functor

### 2.2. Using <*> for partial application in a context

#### 2.2.1. Introducing the <*> operator

#### 2.2.2. Using <*> to finish our city distance program

#### 2.2.3. Using multi-argument function in IO using <$> and <*>

### 2.3. Using <*> to create data in a context

#### 2.3.1. Creating a User in the Context of a Maybe

### 2.4. Exercises

### 2.5. Answers to Spot Quizzes

## 3. Lesson 24 Lists as Context: A deeper look at the Applicative Type Class

### 3.1. Introducing the Applicative type class

#### 3.1.1. The pure method

### 3.2. Containers vs Contexts

### 3.3. List as a Context

#### 3.3.1. Container vs context

#### 3.3.2. A game show example

#### 3.3.3. Generating the First N prime numbers

#### 3.3.4. Quickly generating large amounts of test data

### 3.4. Exercises

### 3.5. Answers to Spot Quizzes

## 4. Lesson 25 Introducing the Monad Type Class

### 4.1. The Limitations of Applicative and Functor

#### 4.1.1. Combining two Map lookups

#### 4.1.2. Writing a not-so-trivial echo IO Action

### 4.2. The bind operator: >>=

### 4.3. The Monad Type class

#### 4.3.1. Using Monad to build a Hello <Name> program

### 4.4. Exercises

### 4.5. Answers to Spot Quizzes

## 5. Lesson 26 Making Monads Easier with Do-notation

### 5.1. Do-notation revisited

### 5.2. Using do-notation to reuse the same code in different contexts

#### 5.2.1. The problem setup

#### 5.2.2. The IO context - Building a command line tool

#### 5.2.3. The Maybe Context - Working with a Map of Candidates

#### 5.2.4. The List Context - Processing a list of Candidates

#### 5.2.5. Putting it all together and writing a Monadic function

### 5.3. Exercises

### 5.4. Answers to Spot Quizzes

## 6. Lesson 27 The List Monad and List Comprehensions

### 6.1. Building lists with The List Monad

#### 6.1.1. The guard Function

### 6.2. List comprehensions

### 6.3. Monads: much more than just lists

### 6.4. Exercises

### 6.5. Answers to Spot Quizzes

# Capstone: SQL-like queries in Haskell

###### C5.1 Getting Started

###### C5.2 Basic queries for our list: Select and Where

###### C5.2.1 Implementing _select

###### C5.2.2 Implementing _where

###### C5.3 Joining Course and Teacher data types

###### C5.4 Building our HINQ interface and example queries

###### C5.5 Making a HINQ type for our queries

###### C5.6 Running our HINQ queries

###### C5.6.1 Using HINQ with Maybe types

###### C5.6.2 Joining multiple lists to get all enrollments

###### C5.7 Summary

###### C5.8 Extending the Exercise

# Unit 6 - Organizing Code

## 1. Lesson 28 Writing Modules

## 2. Lesson 29 Organizing Code Using Stack

## 3. Lesson 30 Testing with Quick Check

# Capstone Exercise: Building a Library for Prime Numbers

# Unit 7 - Practical haskell

## 1. Lesson 31 Handling Errors The Haskell Way

## 2. Lesson 32 Using Databases

## 3. Lesson 33 Working with JSON Data

## 4. Lesson 34 The Coding Interview Haskell Style : Arrays and ST Monad

## 5. Lesson 35 Random Numbers

# Capstone Exercise: A Genetic Algorithm

## What's inside

- Learn the foundations of Functional Programming
- Write safe, predictable code with fewer bugs
- Thinking in types to solve problems
- Understanding Haskell's unique features
- Use Monads to solve practical problems

## About the reader

Readers should have some experience with JavaScript, Python or Ruby. No functional programming or math skills required.

## About the author

**Will Kurt** currently works as a Data Scientist at Quick Sprout. With a formal background in both Computer Science (MS) and English Literature (BA) he is fascinated with explaining complex technical topics as clearly and generally as possible. He has taught a course section on Haskell at the University of Nevada, Reno and given workshops on Functional Programming. He also blogs about probability at CountBayesie.com.

**Manning Early Access Program (MEAP)**Read chapters as they are written, get the finished eBook as soon as itâ€™s ready, and receive the pBook long before it's in bookstores.

- MEAP combo $44.99 pBook + eBook
- MEAP eBook $35.99 pdf + ePub + kindle

**FREE domestic shipping** on three or more pBooks