Reader feedback....2
Contents....4
Authors' preface....21
Acknowledgements....26
Introduction....30
Why This Book....30
A few words to new programmers....35
Haskevangelism....36
What's in this book?....40
Best practices for examples and exercises....44
All You Need is Lambda....49
All You Need is Lambda....50
What is functional programming?....50
What is a function?....52
The structure of lambda terms....55
Beta reduction....58
Multiple arguments....63
Evaluation is simplification....68
Combinators....69
Divergence....71
Summary....72
Chapter Exercises....73
Answers....75
Definitions....79
Follow-up resources....81
Hello, Haskell!....82
Hello, Haskell....83
Interacting with Haskell code....84
Understanding expressions....88
Functions....91
Evaluation....95
Infix operators....98
Declaring values....105
Arithmetic functions in Haskell....115
Parenthesization....126
Let and where....133
Chapter Exercises....138
Definitions....142
Follow-up resources....144
Strings....145
Printing strings....146
A first look at types....146
Printing simple strings....148
Top-level versus local definitions....155
Types of concatenation functions....158
Concatenation and scoping....163
More list functions....167
Chapter Exercises....170
Definitions....176
Basic datatypes....179
Basic Datatypes....180
What are types?....181
Anatomy of a data declaration....181
Numeric types....185
Comparing values....195
Go on and Bool me....200
Tuples....208
Lists....213
Chapter Exercises....215
Definitions....220
Names and variables....223
Types....226
Types....227
What are types for?....228
How to read type signatures....230
Currying....240
Polymorphism....256
Type inference....265
Asserting types for declarations....270
Chapter Exercises....273
Definitions....287
Follow-up resources....294
Typeclasses....295
Typeclasses....296
What are typeclasses?....296
Back to Bool....298
Eq....300
Writing typeclass instances....305
Num....321
Type-defaulting typeclasses....326
Ord....332
Enum....342
Show....344
Read....351
Instances are dispatched by type....352
Gimme more operations....357
Chapter Exercises....362
Chapter Definitions....371
Typeclass inheritance, partial....374
Follow-up resources....374
More functional patterns....376
Make it func-y....377
Arguments and parameters....377
Anonymous functions....387
Pattern matching....392
Case expressions....408
Higher-order functions....413
Guards....425
Function composition....435
Pointfree style....440
Demonstrating composition....444
Chapter Exercises....448
Chapter Definitions....454
Follow-up resources....465
Recursion....467
Recursion....468
Factorial!....469
Bottom....479
Fibonacci numbers....483
Integral division from scratch....489
Chapter Exercises....496
Definitions....503
Lists....505
Lists....506
The list datatype....506
Pattern matching on lists....508
List's syntactic sugar....512
Using ranges to construct lists....513
Extracting portions of lists....517
List comprehensions....525
Spines and nonstrict evaluation....533
Transforming lists of values....548
Filtering lists of values....559
Zipping lists....561
Chapter Exercises....565
Definitions....574
Follow-up resources....577
Folding lists....578
Folds....579
Bringing you into the fold....579
Recursive patterns....582
Fold right....583
Fold left....596
How to write fold functions....609
Folding and evaluation....616
Summary....619
Scans....621
Chapter Exercises....626
Definitions....633
Follow-up resources....637
Algebraic datatypes....638
Algebraic datatypes....639
Data declarations review....640
Data and type constructors....642
Type constructors and kinds....645
Data constructors and values....647
What's a type and what's data?....653
Data constructor arities....659
What makes these datatypes algebraic?....662
newtype....668
Sum types....675
Product types....679
Normal form....684
Constructing and deconstructing values....690
Function type is exponential....715
Higher-kinded datatypes....722
Lists are polymorphic....725
Binary Tree....729
Chapter Exercises....738
Definitions....751
Signaling adversity....752
Signaling adversity....753
How I learned to stop worrying and love Nothing....753
Bleating either....757
Kinds, a thousand stars in your types....768
Chapter Exercises....780
Definitions....796
Building projects....798
Modules....799
Making packages with Stack....801
Working with a basic project....802
Making our project a library....807
Module exports....810
More on importing modules....813
Making our program interactive....821
do syntax and IO....827
Hangman game....832
Step One: Importing modules....835
Step Two: Generating a word list....841
Step Three: Making a puzzle....846
Adding a newtype....858
Chapter exercises....859
Follow-up resources....863
Testing....865
Testing....866
A quick tour of testing for the uninitiated....867
Conventional testing....869
Enter QuickCheck....881
Morse code....894
Arbitrary instances....911
Chapter Exercises....923
Definitions....933
Follow-up resources....934
Monoid, Semigroup....935
Monoids and semigroups....936
What we talk about when we talk about algebras....937
Monoid....938
How Monoid is defined in Haskell....940
Examples of using Monoid....941
Why Integer doesn't have a Monoid....943
Why bother?....949
Laws....951
Different instance, same representation....956
Reusing algebras by asking for algebras....959
Madness....971
Better living through QuickCheck....973
Semigroup....984
Strength can be weakness....989
Chapter exercises....992
Definitions....1003
Follow-up resources....1004
Functor....1005
Functor....1006
What's a functor?....1007
There's a whole lot of fmap goin' round....1010
Let's talk about f, baby....1013
Functor Laws....1027
The Good, the Bad, and the Ugly....1029
Commonly used functors....1035
Transforming the unapplied type argument....1053
QuickChecking Functor instances....1058
Exercises: Instances of Func....1062
Ignoring possibilities....1063
A somewhat surprising functor....1072
More structure, more functors....1076
IO Functor....1078
What if we want to do something different?....1082
Functors are unique to a datatype....1087
Chapter exercises....1089
Definitions....1094
Follow-up resources....1098
Applicative....1100
Applicative....1101
Defining Applicative....1102
Functor vs. Applicative....1105
Applicative functors are monoidal functors....1107
Applicative in use....1115
Applicative laws....1154
You knew this was coming....1163
ZipList Monoid....1168
Chapter Exercises....1183
Definitions....1186
Follow-up resources....1186
Monad....1188
Monad....1189
Sorry — a monad is not a burrito....1189
Do syntax and monads....1202
Examples of Monad use....1211
Monad laws....1236
Application and composition....1247
Chapter Exercises....1254
Definition....1257
Follow-up resources....1259
Applying structure....1260
Applied structure....1261
Monoid....1261
Functor....1269
Applicative....1274
Monad....1281
An end-to-end example: URL shortener....1285
That's a wrap!....1305
Follow-up resources....1306
Foldable....1307
Foldable....1308
The Foldable class....1309
Revenge of the monoids....1310
Demonstrating Foldable instances....1316
Some basic derived operations....1321
Chapter Exercises....1328
Follow-up resources....1329
Traversable....1330
Traversable....1331
The Traversable typeclass definition....1332
sequenceA....1333
traverse....1335
So, what's Traversable for?....1339
Morse code revisited....1340
Axing tedious code....1344
Do all the things....1348
Traversable instances....1352
Traversable Laws....1355
Quality Control....1356
Chapter Exercises....1357
Follow-up resources....1362
Reader....1363
Reader....1364
A new beginning....1365
This is Reader....1375
Breaking down the Functor of functions....1376
But uh, Reader?....1382
Functions have an Applicative too....1385
The Monad of functions....1393
Reader Monad by itself is boring....1399
You can change what comes below, but not above....1402
You tend to see ReaderT, not Reader....1403
Chapter Exercises....1403
Definition....1410
Follow-up resources....1411
State....1412
State....1413
What is state?....1413
Random numbers....1415
The State newtype....1419
Throw down....1422
Write State for yourself....1431
Get a coding job with one weird trick....1433
Chapter exercises....1440
Follow-up resources....1442
Parser combinators....1443
Parser combinators....1444
A few more words of introduction....1446
Understanding the parsing process....1447
Parsing fractions....1464
Haskell's parsing ecosystem....1473
Alternative....1477
Parsing configuration files....1492
Character and token parsers....1508
Polymorphic parsers....1513
Marshalling from an AST to a datatype....1522
Chapter Exercises....1539
Definitions....1548
Follow-up resources....1549
Composing types....1552
Composing types....1553
Common functions as types....1554
Two little functors sittin' in a tree, L-I-F-T-I-N-G....1559
Twinplicative....1562
Twonad?....1564
Exercises: Compose Instances....1566
Monad transformers....1568
IdentityT....1571
Finding a pattern....1590
Monad transformers....1594
Monad transformers....1595
MaybeT....1595
EitherT....1603
ReaderT....1605
StateT....1609
Types you probably don't want to use....1614
Recovering an ordinary type from a transformer....1616
Lexically inner is structurally outer....1618
MonadTrans....1622
MonadIO aka zoom-zoom....1645
Monad transformers in use....1649
Monads do not commute....1665
Transform if you want to....1665
Chapter Exercises....1666
Defintion....1675
Follow-up resources....1676
Nonstrictness....1677
Laziness....1678
Observational Bottom Theory....1679
Outside in, inside out....1681
What does the other way look like?....1685
Can we make Haskell strict?....1686
Call by name, call by need....1705
Nonstrict evaluation changes what we can do....1706
Thunk Life....1708
Sharing is caring....1712
Refutable and irrefutable patterns....1734
Bang patterns....1737
Strict and StrictData....1741
Adding strictness....1743
Chapter Exercises....1750
Follow-up resources....1753
Basic libraries....1755
Basic libraries and data structures....1756
Benchmarking with Criterion....1757
Profiling your programs....1775
Constant applicative forms....1780
Map....1785
Set....1789
Sequence....1792
Vector....1796
String types....1810
Chapter Exercises....1823
Follow-up resources....1827
IO....1829
IO....1830
Where IO explanations go astray....1831
The reason we need this type....1834
Sharing....1836
IO doesn't disable sharing for everything....1843
Purity is losing meaning....1845
IO's Functor, Applicative, and Monad....1848
Well, then, how do we MVar?....1854
Chapter Exercises....1857
Follow-up resources....1858
When things go wrong....1860
Exceptions....1861
The Exception class and methods....1862
This machine kills programs....1873
Want either? Try!....1880
The unbearable imprecision of trying....1885
Why throwIO?....1888
Making our own exception types....1892
Surprising interaction with bottom....1899
Asynchronous Exceptions....1901
Follow-up Reading....1905
Final project....1907
Final project....1908
fingerd....1908
Exploring finger....1910
Slightly modernized fingerd....1920
Chapter Exercises....1935
Welcome to a new way to learn Haskell. Perhaps you are coming to this book frustrated by previous attempts to learn Haskell. Perhaps you have only the faintest notion of what Haskell is. Perhaps you are coming here because you are not convinced that anything will ever be better than Common Lisp/Scala/Ruby/whatever language you love, and you want to argue with us. Perhaps you were just looking for the 18 billionth (n.b.: this number may be inaccurate) monad tutorial, certain that this time around you will understand monads once and for all. Whatever your situation, welcome and read on! It is our goal here to make Haskell as clear, painless, and practical as we can, no matter what prior experiences you’re bringing to the table.