Table Of ContentPractical
Haskell
A Real World Guide to Programming
—
Second Edition
—
Alejandro Serrano Mena
Practical Haskell
A Real World Guide to Programming
Second Edition
Alejandro Serrano Mena
Practical Haskell: A Real World Guide to Programming
Alejandro Serrano Mena
Utrecht, The Netherlands
ISBN-13 (pbk): 978-1-4842-4479-1 ISBN-13 (electronic): 978-1-4842-4480-7
https://doi.org/10.1007/978-1-4842-4480-7
Copyright © 2019 by Alejandro Serrano Mena
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the
material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation,
broadcasting, reproduction on microfilms or in any other physical way, and transmission or information
storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now
known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with
every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an
editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the
trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not
identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to
proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication,
neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or
omissions that may be made. The publisher makes no warranty, express or implied, with respect to the
material contained herein.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Steve Anglin
Development Editor: Matthew Moodie
Coordinating Editor: Mark Powers
Cover designed by eStudioCalamar
Cover image designed by Freepik (www.freepik.com)
Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street,
6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-
sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member
(owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a
Delaware corporation.
For information on translations, please e-mail [email protected]; for reprint, paperback, or audio rights,
please email [email protected].
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and
licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales
web page at http://www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is available to
readers on GitHub via the book’s product page, located at www.apress.com/9781484244791. For more
detailed information, please visit http://www.apress.com/source-code.
Printed on acid-free paper
To Elena, and my two boys Quique and Julio, which bring
me joy every single day.
Table of Contents
About the Author �����������������������������������������������������������������������������������������������������xv
About the Technical Reviewer �������������������������������������������������������������������������������xvii
Acknowledgments ��������������������������������������������������������������������������������������������������xix
Introduction ������������������������������������������������������������������������������������������������������������xxi
Part I: First Steps ��������������������������������������������������������������������������������������������1
Chapter 1: Going Functional �������������������������������������������������������������������������������������3
Why Haskell? ��������������������������������������������������������������������������������������������������������������������������������3
Why Pure Functional Programming? ���������������������������������������������������������������������������������������4
Why Strong Static Typing? �������������������������������������������������������������������������������������������������������6
The Haskell Ecosystem ������������������������������������������������������������������������������������������������������������7
The History of Haskell �������������������������������������������������������������������������������������������������������������������8
Your Working Environment ������������������������������������������������������������������������������������������������������������8
Installing on Windows or Mac OS X �����������������������������������������������������������������������������������������9
Installing on Linux �������������������������������������������������������������������������������������������������������������������9
First Steps with GHCi ������������������������������������������������������������������������������������������������������������������11
The Time Machine Store �������������������������������������������������������������������������������������������������������������13
Summary�������������������������������������������������������������������������������������������������������������������������������������14
Chapter 2: Declaring the Data Model ����������������������������������������������������������������������17
Characters, Numbers, and Lists ��������������������������������������������������������������������������������������������������17
Characters �����������������������������������������������������������������������������������������������������������������������������18
Numbers ��������������������������������������������������������������������������������������������������������������������������������19
Strings �����������������������������������������������������������������������������������������������������������������������������������21
Lists ���������������������������������������������������������������������������������������������������������������������������������������22
v
Table of ConTenTs
Creating a New Project ���������������������������������������������������������������������������������������������������������������27
Creating a Project with Cabal ������������������������������������������������������������������������������������������������27
Creating a Project with Stack ������������������������������������������������������������������������������������������������29
Understanding Modules ���������������������������������������������������������������������������������������������������������31
Cabal and Stack ���������������������������������������������������������������������������������������������������������������������33
Defining Simple Functions ����������������������������������������������������������������������������������������������������������33
Creating a Simple Function ���������������������������������������������������������������������������������������������������34
Specifying the Function’s Type ����������������������������������������������������������������������������������������������34
Developing a Robust Example �����������������������������������������������������������������������������������������������35
Returning More Than One Value ��������������������������������������������������������������������������������������������38
Working with Data Types �������������������������������������������������������������������������������������������������������������40
Pattern Matching �������������������������������������������������������������������������������������������������������������������44
Records ���������������������������������������������������������������������������������������������������������������������������������56
Summary�������������������������������������������������������������������������������������������������������������������������������������62
Chapter 3: Increasing Code Reuse ��������������������������������������������������������������������������63
Parametric Polymorphism �����������������������������������������������������������������������������������������������������������64
Functions As Parameters ������������������������������������������������������������������������������������������������������������67
Higher-Order Functions ���������������������������������������������������������������������������������������������������������68
Anonymous Functions �����������������������������������������������������������������������������������������������������������70
Partial Application of a Function ��������������������������������������������������������������������������������������������73
More on Modules ������������������������������������������������������������������������������������������������������������������������77
Module Imports ���������������������������������������������������������������������������������������������������������������������77
Smart Constructors and Views ����������������������������������������������������������������������������������������������79
Diving into Lists ��������������������������������������������������������������������������������������������������������������������������82
Folds ��������������������������������������������������������������������������������������������������������������������������������������83
Lists and Predicates ��������������������������������������������������������������������������������������������������������������87
Lists Containing Tuples ����������������������������������������������������������������������������������������������������������95
List Comprehensions �������������������������������������������������������������������������������������������������������������96
Haskell Origami �������������������������������������������������������������������������������������������������������������������������101
Summary�����������������������������������������������������������������������������������������������������������������������������������106
vi
Table of ConTenTs
Chapter 4: Using Containers and Type Classes �����������������������������������������������������109
Using Packages �������������������������������������������������������������������������������������������������������������������������110
Managing Dependencies �����������������������������������������������������������������������������������������������������111
Building Packages ���������������������������������������������������������������������������������������������������������������113
Obtaining Help ���������������������������������������������������������������������������������������������������������������������118
Containers: Maps, Sets, Trees, Graphs ��������������������������������������������������������������������������������������118
Maps ������������������������������������������������������������������������������������������������������������������������������������119
Sets �������������������������������������������������������������������������������������������������������������������������������������124
Trees ������������������������������������������������������������������������������������������������������������������������������������126
Graphs ���������������������������������������������������������������������������������������������������������������������������������129
Ad Hoc Polymorphism: Type Classes �����������������������������������������������������������������������������������������132
Declaring Classes and Instances �����������������������������������������������������������������������������������������133
Built-in Type Classes �����������������������������������������������������������������������������������������������������������137
Binary Trees for the Minimum Price ������������������������������������������������������������������������������������������143
Step 1: Simple Binary Trees �������������������������������������������������������������������������������������������������143
Step 2: Polymorphic Binary Trees ����������������������������������������������������������������������������������������145
Step 3: Binary Trees with Monoidal Cache ��������������������������������������������������������������������������147
Container-Related Type Classes ������������������������������������������������������������������������������������������������150
Functors ������������������������������������������������������������������������������������������������������������������������������150
Foldables �����������������������������������������������������������������������������������������������������������������������������153
Summary�����������������������������������������������������������������������������������������������������������������������������������155
Chapter 5: Laziness and Infinite Structures ���������������������������������������������������������157
An Infinite Number of Time Machines ���������������������������������������������������������������������������������������158
Lazy Evaluation Model ��������������������������������������������������������������������������������������������������������������163
Understanding Evaluation in Haskell �����������������������������������������������������������������������������������163
Problems with Laziness �������������������������������������������������������������������������������������������������������168
Pattern Matching and Laziness �������������������������������������������������������������������������������������������171
Profiling with GHC����������������������������������������������������������������������������������������������������������������173
Strictness Annotations ��������������������������������������������������������������������������������������������������������������179
Summary�����������������������������������������������������������������������������������������������������������������������������������182
vii
Table of ConTenTs
Part II: Data Mining �������������������������������������������������������������������������������������183
Chapter 6: Knowing Your Clients Using Monads ���������������������������������������������������185
Data Mining �������������������������������������������������������������������������������������������������������������������������������186
Implementing K-means �������������������������������������������������������������������������������������������������������186
Lenses ���������������������������������������������������������������������������������������������������������������������������������193
Discovering Monads ������������������������������������������������������������������������������������������������������������������202
Watching Out for Incomplete Data ���������������������������������������������������������������������������������������202
Combinators for State ���������������������������������������������������������������������������������������������������������205
Dissecting the Combinators ������������������������������������������������������������������������������������������������209
do Notation ��������������������������������������������������������������������������������������������������������������������������212
Monad Laws ������������������������������������������������������������������������������������������������������������������������216
Different Sorts of State �������������������������������������������������������������������������������������������������������������217
State and Lenses �����������������������������������������������������������������������������������������������������������������218
Reader, Writer, and RWS ������������������������������������������������������������������������������������������������������220
Mutable References with ST �����������������������������������������������������������������������������������������������224
Summary�����������������������������������������������������������������������������������������������������������������������������������227
Chapter 7: More Monads: Now for Recommendations �����������������������������������������229
Returning More Than One Value ������������������������������������������������������������������������������������������������230
The List Monad ��������������������������������������������������������������������������������������������������������������������230
A New View Over Monads ����������������������������������������������������������������������������������������������������232
Failures and Alternatives �����������������������������������������������������������������������������������������������������233
Association Rules Learning �������������������������������������������������������������������������������������������������������237
Flattening Values into Transactions �������������������������������������������������������������������������������������238
The Apriori Algorithm �����������������������������������������������������������������������������������������������������������241
Search Problems �����������������������������������������������������������������������������������������������������������������������247
Paths in a Graph ������������������������������������������������������������������������������������������������������������������247
The Logic Monad �����������������������������������������������������������������������������������������������������������������249
Monads and Lists Redux �����������������������������������������������������������������������������������������������������������252
Combining Values Under a Monad ���������������������������������������������������������������������������������������252
Monad Comprehensions ������������������������������������������������������������������������������������������������������256
viii
Table of ConTenTs
Combining Monads �������������������������������������������������������������������������������������������������������������������259
Monad Transformers ������������������������������������������������������������������������������������������������������������260
Monad Classes ��������������������������������������������������������������������������������������������������������������������265
Summary�����������������������������������������������������������������������������������������������������������������������������������268
Chapter 8: Working in Several Cores ��������������������������������������������������������������������269
Parallelism, Concurrency, and Distribution �������������������������������������������������������������������������������269
The Par Monad ��������������������������������������������������������������������������������������������������������������������������271
Futures ��������������������������������������������������������������������������������������������������������������������������������271
Dataflow Parallelism with IVars �������������������������������������������������������������������������������������������274
Parallelizing the Apriori Algorithm ���������������������������������������������������������������������������������������276
Software Transactional Memory �����������������������������������������������������������������������������������������������279
Concurrent Use of Resources ����������������������������������������������������������������������������������������������280
Atomic Transactions ������������������������������������������������������������������������������������������������������������282
Rolling Back Transactions ����������������������������������������������������������������������������������������������������286
Producer-Consumer Queues �����������������������������������������������������������������������������������������������������288
Single-Process Queues �������������������������������������������������������������������������������������������������������288
Message Queues Using AMQP ���������������������������������������������������������������������������������������������290
AMQP in Haskell ������������������������������������������������������������������������������������������������������������������293
Summary�����������������������������������������������������������������������������������������������������������������������������������296
Part III: Resource Handling �������������������������������������������������������������������������299
Chapter 9: Dealing with Files: IO and Conduit ������������������������������������������������������301
Basic Input and Output ��������������������������������������������������������������������������������������������������������������302
Randomness �����������������������������������������������������������������������������������������������������������������������������307
Working with Files ��������������������������������������������������������������������������������������������������������������������310
Reading and Writing ������������������������������������������������������������������������������������������������������������310
Handling Files ����������������������������������������������������������������������������������������������������������������������314
Error Handling ���������������������������������������������������������������������������������������������������������������������������316
Pure Errors ��������������������������������������������������������������������������������������������������������������������������316
Catching Exceptions ������������������������������������������������������������������������������������������������������������319
Throwing Exceptions �����������������������������������������������������������������������������������������������������������324
ix
Table of ConTenTs
Streaming Data with Conduit ����������������������������������������������������������������������������������������������������326
Problems with Lazy Input/Output ����������������������������������������������������������������������������������������326
Introducing Conduits �����������������������������������������������������������������������������������������������������������328
Accessing Files via Conduit �������������������������������������������������������������������������������������������������332
Looking Further Than Text Files ������������������������������������������������������������������������������������������������335
Basic Networking ����������������������������������������������������������������������������������������������������������������335
Binary Serialization �������������������������������������������������������������������������������������������������������������337
Comma-Separated Values ���������������������������������������������������������������������������������������������������339
Summary�����������������������������������������������������������������������������������������������������������������������������������341
Chapter 10: Building and Parsing Text �����������������������������������������������������������������343
The Five Textual Data Types ������������������������������������������������������������������������������������������������������343
Building as Fast as the Wind �����������������������������������������������������������������������������������������������������349
Parsing with attoparsec ������������������������������������������������������������������������������������������������������������353
Introducing New Type Classes ��������������������������������������������������������������������������������������������������360
Applicative ���������������������������������������������������������������������������������������������������������������������������361
Functors, Applicatives, and Monads ������������������������������������������������������������������������������������363
Alternative ���������������������������������������������������������������������������������������������������������������������������366
Traversable ��������������������������������������������������������������������������������������������������������������������������367
Don’t Overengineer: Just Use JSON ������������������������������������������������������������������������������������������370
Summary�����������������������������������������������������������������������������������������������������������������������������������378
Chapter 11: Safe Database Access �����������������������������������������������������������������������379
Database Access Landscape�����������������������������������������������������������������������������������������������������379
Abstracting Over Several DBMSs ����������������������������������������������������������������������������������������380
Introducing Persistent and Esqueleto ����������������������������������������������������������������������������������381
Connection ��������������������������������������������������������������������������������������������������������������������������������382
Schemas and Migrations ����������������������������������������������������������������������������������������������������������385
Describing the Entities ��������������������������������������������������������������������������������������������������������386
Creating the Database ���������������������������������������������������������������������������������������������������������391
x
Description:Get a practical, hands-on introduction to the Haskell language, its libraries and environment, and to the functional programming paradigm that is fast growing in importance in the software industry. This book contains excellent coverage of the Haskell ecosystem and supporting tools, include Cabal an