Get Programming with Haskell
Will Kurt
  • March 2018
  • ISBN 9781617293764
  • 616 pages
  • printed in black & white

An approachable and thorough introduction to Haskell and functional programming. This book will change the way you think about programming for good.

Makarand Deshpande, SAS R&D

Get Programming with Haskell introduces you to the Haskell language without drowning you in academic jargon and heavy functional programming theory. By working through 42 easy-to-follow lessons, you'll learn Haskell the best possible way—by doing Haskell!

Table of Contents detailed table of contents

Lesson 1 Getting Started with Haskell

Welcome to Haskell

The Haskell Platform

Text Editors

The Glasgow Haskell Compiler

Interacting with Haskell - GHCi

Writing and working with Haskell code

Summary

Answers to quick checks

Unit 1: Foundations of Functional programming

Lesson 2 Functions and functional programming

Functions

Functional Programming

The Value of Functional Programming in Practice

Variables

Variables that are Variable

Summary

Answers to Quick Checks

Lesson 3 Lambda functions and lexical scope

Lambda Functions

Writing your own Where clause

From Lambda to Let: Making your own variable variables!

Practical Lambda Functions and Lexical Scope

Summary

Answers to Quick Checks

Lesson 4 First-class functions

Functions as Arguments

Lambda Functions as arguments

Returning Functions

Summary

Answers to Quick checks

Lesson 5 Closures and partial application

Closures - Creating Functions with Functions

Example: Generating URLs for an API

Partial Application: Making Closures Simple

Putting it all together

Summary

Answers to Quick checks

Lesson 6 Lists

The anatomy of a list

Lists and Lazy Evaluation

Common Functions on Lists

The !! operator

length

reverse

elem

take and drop

zip

cycle

Summary

Answers to Quick Checks

Lesson 7 Rules for recursion and pattern matching

Recursion

Rules for recursion

Rule 1: Identify the end goal(s)

Rule 2: Determine what happens when a goal is reached

Rule 3: List all alternate possibilities

Rule 4: Determine your ?Rinse and Repeat?

Rule 5: Ensure that each alterative moves you toward the goal

Your first recursive function: greatest common divisor

Summary

Answers to Quick Checks

Lesson 8 Writing recursive functions

Review: Rules of Recursion

Recursion on Lists

Implementing length

Implementing take

Implementing cycle

Pathological Recursion: Ackerman Function and the Collatz Conjecture

The Ackermann function

The Collatz conjecture

Summary

Answers to Quick checks

Lesson 9 Higher-order functions

Using Map

Abstracting away recursion with map

Filtering a list

Folding a list

Summary

Answers to Quick Checks

Lesson 10 Capstone: Functional object-oriented programming with robots!

An object with one property: A cup of coffee

Creating a constructor

Adding accessors to your object

A more complex object: Let's build fighting robots!

Sending messages between objects

Why Stateless programming matters

Types - Objects and so much more!

Summary

Unit 2: Introducing types

Lesson 11 Type basics

Types in Haskell

Function Types

Functions for converting to and from Strings

Functions with Multiple Arguments

Types for First Class functions

Type Variables

Summary

Answers to Quick checks

Lesson 12 Creating your own types

Using Type Synonyms

Creating new types

Using Record Syntax

Summary

Answers to Quick checks

Lesson 13 Type classes

Further Exploring Types

Type classes

The Benefits of Type Classes

Defining a Type class

Common Type Classes

The Ord and Eq Type classes

Bounded

Show

Deriving Type classes

Summary

Answers to Quick checks

Lesson 14 Using type classes

A type in need of classes

Implementing Show

Type Classes and Polymorphism

Default Implementation and Minimum complete definitions

Implementing Ord

To Derive or Not to Derive?

Type classes for more complex types

Type class roadmap

Summary

Answers to Quick checks

Lesson 15 Capstone: Secret messages!

Ciphers for Beginners: ROT13

Implementing your own ROT Cipher

The rotN algorithm

Rot encoding a String

The problem with decoded odd-sized alphabets

XOR: the magic of cryptography!

Representing values as Bits

The one-time pad

Implementing your one-time pad

A Cipher Class

Summary

Extending the exercise

Unit 3: Programming in Types

Lesson 16 Creating types with ?and? and ?or?

Product Types - Combing types with ?and?

The curse of product types: Hierarchical design

Sum types?Combining Types with ?or?

Putting together your books store

Summary

Answers to Quick checks

Lesson 17 Design by Composition?Semigroups and Monoids

Intro to composability - Combining Functions

Combining like Types: Semigroups

The Color Semigroup

Making Color associative and using Guards

Composing with Identity: Monoids

mconcat: Combining multiple Monoids at once

Monoid Laws

Practical Monoids?Building Probability Tables

Summary

Answers to Quick checks

Lesson 18 Parameterized types

Types that take arguments

A more useful parameterized type: Triple

Lists

Types with more than one parameter

Tuples

Kinds: Types of Types

Data.Map

Summary

Answers to Quick checks

Lesson 19 The Maybe type: dealing with missing values

Introducing Maybe : Solving missing values with types

The problem with Null

Handling missing values with errors

Returning Null values

Using Maybe as a solution to missing values

Computing with Maybe

Back to the Lab! More complex computation with Maybe

Summary

Answers to Spot Quizzes

Lesson 20 Capstone: Time Series

Your data and the TS data type

Building a Basic Time-Series Type

Stitching together TS data with Semigroup and Monoid

Making TS an Instance of Monoid

Performing calculations on your Time Series

Calculating the min and max values for your time series

Transforming Time Series

Moving average

Summary

Extending the exercise

Unit 4: IO in Haskell

Lesson 21 Hello World!?Introducing IO types

IO Types - Dealing with an impure world

Examples of IO Actions

Keeping values in the context of IO

Do-notation

An example: Command line Pizza cost calculator

A peak at Monads?Do-notation in Maybe

Summary

Answers to Quick checks

Lesson 22 Interacting with the command line and lazy I/O

Interacting with the command line the nonlazy way

Interacting with Lazy I/O

Thinking of your problem as a lazy list

Summary

Answers to Spot Quizzes

Lesson 23 Working with text and Unicode

The Text Type

When to use Text vs. String

Using Data.Text

OverloadedStrings and Haskell extensions

Basic Text utilities

Text and Unicode

Searching Sanskrit

Text I/O

Summary

Answers to Quick checks

Lesson 24 Working with files

Opening and closing files

Simple I/O tools

The Trouble with Lazy I/O

Strict I/O

When to use Lazy vs. Strict

Summary

Answers to Spot Quizzes

Lesson 25 Working with binary data

Working with binary data by using ByteString

Glitching JPEGS

Inserting Random Bytes

Sorting random bytes

Chaining together IO Actions with foldM

ByteStrings, Char8, and Unicode

Summary

Answers to Quick checks

Lesson 26 Capstone: Processing binary files and book data

Working with Book Data

Working with MARC Records

Understanding the Structure of a MARC record

Getting the data

Checking the Leader and Iterating through your records

Reading the Directory

Using the directory to look up fields

Processing the directory entries and looking up MARC Fields

Getting Author and Title information from a Marc Field

Putting it all together

Summary

Extending the Exercise

Unit 5: Working with type in a context

Lesson 27 The Functor type class

An Example: Computing in a Maybe

Using functions in context with the Functor Type Class

Functors are everywhere!

One interface for Four Problems

Converting a Maybe RobotPart to Maybe Html

Converting a list of RobotParts to a List of Html

Converting A Map of RobotParts to Html

Transforming an IO RobotPart into IO Html

Summary

Answers to Quick checks

Lesson 28 A peek at the Applicative type class: using functions in a context

A command line application for calculating the distance between cities

The limitations of Functor

Using <*> for partial application in a context

Introducing the <*> operator

Using <*> to finish your city distance program

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

Using <*> to create data in a context

Creating a User in the Context of a Maybe

Summary

Answers to Quick checks

Lesson 29 Lists as a Context: A deeper look at the Applicative type class

Introducing the Applicative type class

The pure method

Containers vs. Contexts

List as a Context

Exploring Container vs. Context with a list

game show example

Generating the First N prime numbers

Quickly generating large amounts of test data

Summary

Answers to Quick checks

Lesson 30 Introducing the Monad type class

The Limitations of Applicative and Functor

Combining two Map lookups

Writing a not-so-trivial echo IO Action

The bind operator: >>=

The Monad Type class

Using Monad to build a Hello <Name> program

Summary

Answers to Quick checks

Lesson 31 Making Monads easier with do-notation

Do-notation revisited

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

The problem setup

The IO context?Building a command line tool

The Maybe Context?Working with a Map of Candidates

The List Context?Processing a list of Candidates

Putting it all together and writing a Monadic function

Summary

Answers to Quick checks

Lesson 32 The List Monad and List comprehensions

Building lists with The List Monad

The guard Function

List comprehensions

Monads: much more than just lists

Summary

Answers to Quick checkes

Lesson 33 Capstone: SQL-like queries in Haskell

Getting Started

Basic queries for your list: Select and Where

Implementing _select

Implementing _where

Joining Course and Teacher data types

Building your HINQ interface and example queries

Making a HINQ type for your queries

Running your HINQ queries

Using HINQ with Maybe types

Joining multiple lists to get all enrollments

Summary

Extending the Exercise

Unit 6 : Organizing code and building Projects

Lesson 34 Organizing Haskell code with modules

What happens when you write a function with same name as one in Prelude?

Building a multifile program with modules

Creating The Main module

Putting your improved isPalindrome code in its own module

Using your Palindrome module in your Main module

Summary

Answers to Quick Checks

Lesson 35 Building projects with stack

Starting a new stack project

Understanding the project structure

The project .cabal file and autogenerated files

The app, src and test directories

Writing your code

Building and running your project!

A quick improvement: getting rid of language pragmas

Summary

Answers to Quick Checks

Lesson 36 Property testing with QuickCheck

Starting a new project

Different types of testing

Manual testing and calling GHCi from stack

Writing your own unit tests and using stack test

Property testing QuickCheck

Testing properties

Introducing QuickCheck

Using QuickCheck with more types and installing packages

Summary

Answers to Quick checks

Lesson 37 Capstone: Building a prime-number library

Starting your new project

Modifying the default files

Writing your core library functions

Defining primes

Defining an isPrime function

Writing tests for your code

Defining Properties for isPrime

Fixing the Bug

Adding code to factor numbers

Summary

Extending this exercise

Unit 7 Practical Haskell

Lesson 38 Errors in Haskell and the Either Type

Head, Partial Functions and Errors

Head and Partial Functions

Handling Partial functions with Maybe

Introducing the Either type

Building a prime check with Either

Summary

Answers to Quick checks

Lesson 39 Making HTTP requests in Haskell

Getting your project set up

Your starter code

Using the HTTP.Simple module

Making an HTTP Request

Putting it all together

Summary

Answers to Quick checks

Lesson 40 Working with JSON data using Aeson

Getting set up

Setting up stack

Using the Aeson library

Making your Data types instances of FromJSON and ToJSON

The easy way

Writing your own instances of FromJSON and ToJSON

Putting it all together: Reading your NOAA Data

Summary

Answers to Quick checks

Lesson 41 Using databases in Haskell

Setting up your project

Using SQLite and setting up your database

Your Haskell Data

Creating data?Inserting Users and Checking out tools

Adding new users to your data base

Creating checkouts

Reading data from the database and FromRow

Making your data an instance of FromRow

Listing Users and Tools

Updating existing data

Deleting data from your database

Putting it all together

Summary

Answers to Quick checks

Lesson 42 Efficient, stateful arrays in Haskell

Creating Efficient Arrays in Haskell with the UArray type

The inefficiencies of lazy lists

Creating a UArray

Updating your UArray

Mutating state with STUArray

Taking values out of the ST context

Implementing a Bubble sort

Summary

Answers to Quick checks

About the Technology

Programming languages often differ only around the edges—a few keywords, libraries, or platform choices. Haskell gives you an entirely new point of view. To the software pioneer Alan Kay a change in perspective can be worth 80 IQ points, and Haskellers agree on the dramatic benefits of thinking the Haskell way—thinking functionally, with type safety, mathematical certainty, and more. In this hands-on book, that's exactly what you'll learn to do.

About the book

Get Programming with Haskell leads you through short lessons, examples, and exercises designed to make Haskell your own. It has crystal-clear illustrations and guided practice. You will write and test dozens of interesting programs and dive into custom Haskell modules. You will gain a new perspective on programming plus the practical ability to use Haskell in the everyday world. (The 80 IQ points: not guaranteed!)

What's inside

  • Thinking in Haskell
  • Functional programming basics
  • Programming in types
  • Real-world applications for Haskell

About the reader

Written for readers who know one or more programming languages.

About the author

Will Kurt currently works as a data scientist. He writes a blog at www.countbayesie.com, explaining data science to normal people.


buy
Get Programming with Haskell (combo) added to cart
continue shopping
go to cart

combo $44.99 pBook + eBook + liveBook
Get Programming with Haskell (eBook) added to cart
continue shopping
go to cart

eBook $35.99 pdf + ePub + kindle + liveBook

FREE domestic shipping on three or more pBooks

I've been trying to crack the tough nut that is Haskell for a while; I tried other books, but this was the first one that actually allowed me to understand how to use Haskell. I love how the author mixes theory with a lot of practical exercises.

Victor Tatai, Fitbit

More than a beginner's book. Full of insightful examples that make your Haskell thinking click.

Carlos Aya, CO Zero

I thought Haskell was hard to learn. With this book, honestly, it isn't.

Mikkel Arentoft, Danske Bank

A gentle yet definitive introduction to Haskell.

Nikita Dyumin, AppliedTech