List of Figures

Lesson 2. Functions and functional programming

Figure 2.1. Defining a simple function

Lesson 3. Lambda functions and lexical scope

Figure 3.1. The simple function rewritten as a lambda function

Figure 3.2. How sumSquareOrSquareSum works using a lambda function

Figure 3.3. The sumSquareOrSquareSum function rewritten to use a let expression

Figure 3.4. Lexical scope with add1, add2, and add3

Lesson 5. Closures and partial application

Figure 5.1. The genIfEven function lets you build ifEvenX functions simply.

Figure 5.2. ifEvenInc with closure

Figure 5.3. Parts of a URL

Figure 5.4. Capturing the host value in a closure

Figure 5.5. Visualizing partial application

Figure 5.6. The flipBinaryArgs function

Lesson 6. Lists

Figure 6.1. A list is made up of the head element and the tail list.

Lesson 7. Rules for recursion and pattern matching

Figure 7.1. Visualizing pattern-matching internals for myHead

Lesson 8. Writing recursive functions

Figure 8.1. Visualizing collatz path lengths

Lesson 9. Higher-order functions

Figure 9.1. Visualizing foldl (+)

Figure 9.2. Visualizing foldl rcons

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

Figure 10.1. Method-calling approach to OOP

Figure 10.2. Message-passing approach to OOP (commonly used in functional programming languages)

Lesson 11. Type basics

Figure 11.1. Type signature for a variable

Figure 11.2. Defining the double function by using a type signature

Figure 11.3. Type signature for multi-argument functions and definition makeAddress

Figure 11.4. Desugaring the multi-argument makeAddress into a sequence of single-argument functions

Figure 11.5. Visualizing type variables taking on actual values

Lesson 12. Creating your own types

Figure 12.1. Defining the Sex type

Figure 12.2. Combining ABOType and RhType to create BloodType

Lesson 13. Type classes

Figure 13.1. Structure of a type class definition

Lesson 14. Using type classes

Figure 14.1. Visualizing polymorphism for read

Figure 14.2. Type class road map

Lesson 15. Capstone: Secret messages!

Figure 15.1. The best way to visualize ROT13 is as a decoder ring.

Figure 15.2. The symmetric nature of XOR

Figure 15.3. Comparing rotN encoding to XOR on an image

Lesson 17. Design by composition—Semigroups and Monoids

Figure 17.1. Using guards in howMuch

Lesson 18. Parameterized types

Figure 18.1. Definition of the Box parameterized type

Figure 18.2. The definition of a List

Figure 18.3. Using a qualified import

Figure 18.4. The fromList function for building a Map

Lesson 20. Capstone: Time series

Figure 20.1. An example of time-series data for sales

Figure 20.2. Combining two time series

Figure 20.3. Visualizing the diff function

Figure 20.4. Sales time series with diff applied

Figure 20.5. Sales time-series with a moving average of 12 applied

Lesson 21. Hello World!—introducing IO types

Figure 21.1. Treating an IO String like a regular String using do-notation

Lesson 24. Working with files

Figure 24.1. The problem with closing a file before we use it when using lazy evaluation

Lesson 25. Working with binary data

Figure 25.1. A scene from Michael Betancourt’s glitch art video “Kodak Moment” (2013)

Figure 25.2. The target of your glitching is the lovecraft.jpg image.

Figure 25.3. The underwhelming effect of changing a single byte

Figure 25.4. A much more interesting result, achieved with randomSortSection

Figure 25.5. Now your beloved author looks more like a resident of Innsmouth!

Lesson 26. Capstone: Processing binary files and book data

Figure 26.1. Your book data rendered as HTML

Figure 26.2. The content of a raw MARC record

Figure 26.3. Annotated version of the MARC record

Figure 26.4. The leader in your record highlighted

Figure 26.5. An example title subfield a. Notice that a is the first character of the title text you receive.

Lesson 27. The Functor type class

Figure 27.1. The type signature for the fmap function

Figure 27.2. Visualizing how fmap, also <$>, works as an adapter, allowing you to work with types in a context.

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

Figure 28.1. The signature of the function you need to connect your locationsDB with printDistance

Figure 28.2. Annotated type signature for Functor’s only method, fmap

Figure 28.3. You need a new type of adapter for connecting types in a context with functions in a context.

Figure 28.4. Annotated type signature for the <*> operator

Figure 28.5. Combining <$> with <*> to compute haversine in a Maybe context

Lesson 29. Lists as context: a deeper look at the Applicative type class

Figure 29.1. Functor is the superclass of applicative. Figure 29.2 provides the definition of Applicative with its two required methods.

Figure 29.2. Type class definition for Applicative

Figure 29.3. The pure method means you always have a way to take an ordinary type and put it in a context.

Figure 29.4. Thinking of lists as nondeterministic computing is hard because you normally think deterministically.

Figure 29.5. Nondeterministic computing computes on all possible paths, rather than just one.

Lesson 30. Introducing the Monad type class

Figure 30.1. A visualization of the mismatch between function and context that Functor solves

Figure 30.2. Applicative's <*> solves the problem of functions themselves being in a context.

Figure 30.3. Applicative’s pure method means you can always put a result into a context.

Figure 30.4. This is the only pattern you need a solution for; the Monad type class provides an answer.

Figure 30.5. Functor is a superclass of Applicative, which is a superclass of Monad.

Figure 30.6. The >> operator has a strange type signature but is useful for Monads with side effects.

Figure 30.7. Using a lambda expression with return to transform a type a -> a into a -> m a

Lesson 31. Making Monads easier with do-notation

Figure 31.1. Mapping Monad methods to do-notation

Figure 31.2. Desugaring do-notation

Lesson 32. The list monad and list comprehensions

Figure 32.1. Generating lists by thinking of List as a Monad

Figure 32.2. List comprehensions simplify do-notation even further for generating lists.

Lesson 33. Capstone: SQL-like queries in Haskell

Figure 33.1. The relationships among the basic data you’ll be working with

Figure 33.2. The type signature of your select function

Figure 33.3. The type signature for your where function

Figure 33.4. Reading the type signature for _join

Figure 33.5. The _join function joins two data sets on matching properties.

Figure 33.6. Understanding the HINQ data type

Lesson 37. Capstone: Building a prime-number library

Figure 37.1. The files created by stack

Lesson 38. Errors in Haskell and the Either type

Figure 38.1. How can you solve the case of calling head on an empty list?

Lesson 40. Working with JSON data by using Aeson

Figure 40.1. An example of JSON data from the Google Analytics API

Lesson 41. Using databases in Haskell

Figure 41.1. The setup of your database

Lesson 42. Efficient, stateful arrays in Haskell

Figure 42.1. The STUArray is a context that allows for stateful mutations.

Figure 42.2. Unlike IO, you can take values out of the STUArray context.

Figure 42.3. The bubble sort algorithm