100% found this document useful (2 votes)
8 views

Practical Haskell: A Real-World Guide to Functional Programming, 3rd Edition Alejandro Serrano Mena instant download

The document provides information about the book 'Practical Haskell: A Real-World Guide to Functional Programming, 3rd Edition' by Alejandro Serrano Mena, including its ISBNs and copyright details. It also lists additional related books on Haskell and functional programming available for download. The content includes a detailed table of contents outlining the chapters and topics covered in the book.

Uploaded by

azzadeyrineo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
8 views

Practical Haskell: A Real-World Guide to Functional Programming, 3rd Edition Alejandro Serrano Mena instant download

The document provides information about the book 'Practical Haskell: A Real-World Guide to Functional Programming, 3rd Edition' by Alejandro Serrano Mena, including its ISBNs and copyright details. It also lists additional related books on Haskell and functional programming available for download. The content includes a detailed table of contents outlining the chapters and topics covered in the book.

Uploaded by

azzadeyrineo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

Practical Haskell: A Real-World Guide to

Functional Programming, 3rd Edition Alejandro


Serrano Mena pdf download

https://ebookmeta.com/product/practical-haskell-a-real-world-
guide-to-functional-programming-3rd-edition-alejandro-serrano-
mena/

Download more ebook from https://ebookmeta.com


We believe these products will be a great fit for you. Click
the link to download now, or visit ebookmeta.com
to discover even more!

Effective Haskell Solving Real World Problems with


Strongly Typed Functional Programming 1st Edition
Rebecca Skinner

https://ebookmeta.com/product/effective-haskell-solving-real-
world-problems-with-strongly-typed-functional-programming-1st-
edition-rebecca-skinner/

Learn Physics with Functional Programming A Hands on


Guide to Exploring Physics with Haskell 1st Edition
Scott N. Walck

https://ebookmeta.com/product/learn-physics-with-functional-
programming-a-hands-on-guide-to-exploring-physics-with-
haskell-1st-edition-scott-n-walck/

Real World OCaml Functional Programming for the Masses


2nd Edition Anil Madhavapeddy

https://ebookmeta.com/product/real-world-ocaml-functional-
programming-for-the-masses-2nd-edition-anil-madhavapeddy/

Project Management 1st Edition Dk

https://ebookmeta.com/product/project-management-1st-edition-dk/
Trump’s America and International Relations in the
Indo-Pacific: Theoretical Analysis of Changes &
Continuities (Contributions to International Relations)
Tsuneo Akaha (Editor)
https://ebookmeta.com/product/trumps-america-and-international-
relations-in-the-indo-pacific-theoretical-analysis-of-changes-
continuities-contributions-to-international-relations-tsuneo-
akaha-editor/

Patterns of religious commitment Volume 1 American


Piety The Nature of Religious Commitment

https://ebookmeta.com/product/patterns-of-religious-commitment-
volume-1-american-piety-the-nature-of-religious-commitment/

Death in the Diaspora British and Irish Gravestones 1st


Edition Nicholas Evans Editor Angela Mccarthy Editor

https://ebookmeta.com/product/death-in-the-diaspora-british-and-
irish-gravestones-1st-edition-nicholas-evans-editor-angela-
mccarthy-editor/

Cambridge IGCSE® & O Level Complete Physics Student


Book Fourth Edition Stephen Pople

https://ebookmeta.com/product/cambridge-igcse-o-level-complete-
physics-student-book-fourth-edition-stephen-pople/

Lead from the Start How to Succeed As the New Principal


of Your School a School Leadership Guide for New
Principals and Experienced Educators 1st Edition Tommy
Reddicks
https://ebookmeta.com/product/lead-from-the-start-how-to-succeed-
as-the-new-principal-of-your-school-a-school-leadership-guide-
for-new-principals-and-experienced-educators-1st-edition-tommy-
Daughter of the Pirate King 1st Edition Levenseller
Tricia

https://ebookmeta.com/product/daughter-of-the-pirate-king-1st-
edition-levenseller-tricia/
Practical Haskell
A Real-World Guide to Functional
Programming
Third Edition

Alejandro Serrano Mena


Practical Haskell: A Real-World Guide to Functional Programming
Alejandro Serrano Mena
Utrecht, The Netherlands

ISBN-13 (pbk): 978-1-4842-8580-0 ISBN-13 (electronic): 978-1-4842-8581-7


https://doi.org/10.1007/978-1-4842-8581-7
Copyright © 2022 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: James Markham
Coordinating Editor: Gryffin Winkler
Cover designed by eStudioCalamar
Cover image by Shubham Dhage on Unsplash (www.unsplash.com)
Distributed to the book trade worldwide by Apress Media, LLC, 1 New York Plaza, New York, NY 10004,
U.S.A. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], 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 e-mail [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 (https://github.com/Apress). 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,
who 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������������������������������������������������������������������������������������������������������������ 9
First Steps with GHCi������������������������������������������������������������������������������������������������������������������ 10
The Time Machine Store������������������������������������������������������������������������������������������������������������� 13
Summary������������������������������������������������������������������������������������������������������������������������������������ 14

Chapter 2: Declaring the Data Model���������������������������������������������������������������������� 15


Characters, Numbers, and Lists�������������������������������������������������������������������������������������������������� 15
Characters����������������������������������������������������������������������������������������������������������������������������� 16
Numbers�������������������������������������������������������������������������������������������������������������������������������� 17
Strings����������������������������������������������������������������������������������������������������������������������������������� 19
Lists��������������������������������������������������������������������������������������������������������������������������������������� 20
Creating a New Project��������������������������������������������������������������������������������������������������������������� 25
Creating a Project with Cabal������������������������������������������������������������������������������������������������ 25
Creating a Project with Stack������������������������������������������������������������������������������������������������ 27

v
Table of Contents

Understanding Modules��������������������������������������������������������������������������������������������������������� 29
Cabal and Stack��������������������������������������������������������������������������������������������������������������������� 31
Defining Simple Functions���������������������������������������������������������������������������������������������������������� 32
Creating a Simple Function��������������������������������������������������������������������������������������������������� 32
Specifying the Function’s Type���������������������������������������������������������������������������������������������� 33
Developing a Robust Example����������������������������������������������������������������������������������������������� 33
Returning More Than One Value�������������������������������������������������������������������������������������������� 36
Working with Data Types������������������������������������������������������������������������������������������������������������� 38
Pattern Matching������������������������������������������������������������������������������������������������������������������� 43
Records��������������������������������������������������������������������������������������������������������������������������������� 54
Summary������������������������������������������������������������������������������������������������������������������������������������ 60

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�������������������������������������������������������������������������������������������� 72
More on Modules������������������������������������������������������������������������������������������������������������������������ 76
Module Imports��������������������������������������������������������������������������������������������������������������������� 77
Smart Constructors and Views���������������������������������������������������������������������������������������������� 79
Diving into Lists�������������������������������������������������������������������������������������������������������������������������� 82
Folds�������������������������������������������������������������������������������������������������������������������������������������� 82
Lists and Predicates�������������������������������������������������������������������������������������������������������������� 87
Lists Containing Tuples���������������������������������������������������������������������������������������������������������� 94
List Comprehensions������������������������������������������������������������������������������������������������������������� 96
Haskell Origami������������������������������������������������������������������������������������������������������������������������� 101
Summary���������������������������������������������������������������������������������������������������������������������������������� 106

vi
Table of Contents

Chapter 4: Using Containers and Type Classes����������������������������������������������������� 107


Using Packages������������������������������������������������������������������������������������������������������������������������� 107
Managing Dependencies����������������������������������������������������������������������������������������������������� 108
Building Packages��������������������������������������������������������������������������������������������������������������� 111
Obtaining Help��������������������������������������������������������������������������������������������������������������������� 116
Containers: Maps, Sets, Trees, Graphs�������������������������������������������������������������������������������������� 116
Maps������������������������������������������������������������������������������������������������������������������������������������ 117
Sets������������������������������������������������������������������������������������������������������������������������������������� 122
Trees������������������������������������������������������������������������������������������������������������������������������������ 124
Graphs��������������������������������������������������������������������������������������������������������������������������������� 127
Ad Hoc Polymorphism: Type Classes����������������������������������������������������������������������������������������� 130
Declaring Classes and Instances����������������������������������������������������������������������������������������� 130
Built-In Type Classes����������������������������������������������������������������������������������������������������������� 135
Binary Trees for the Minimum Price������������������������������������������������������������������������������������������ 141
Step 1: Simple Binary Trees������������������������������������������������������������������������������������������������� 141
Step 2: Polymorphic Binary Trees���������������������������������������������������������������������������������������� 143
Step 3: Binary Trees with Monoidal Cache�������������������������������������������������������������������������� 145
Container-Related Type Classes������������������������������������������������������������������������������������������������ 148
Functors������������������������������������������������������������������������������������������������������������������������������ 148
Foldables����������������������������������������������������������������������������������������������������������������������������� 151
Summary���������������������������������������������������������������������������������������������������������������������������������� 153

Chapter 5: Laziness and Infinite Structures��������������������������������������������������������� 155


An Infinite Number of Time Machines��������������������������������������������������������������������������������������� 156
Lazy Evaluation Model�������������������������������������������������������������������������������������������������������������� 161
Understanding Evaluation in Haskell����������������������������������������������������������������������������������� 161
Problems with Laziness������������������������������������������������������������������������������������������������������� 166
Pattern Matching and Laziness������������������������������������������������������������������������������������������� 169
Profiling with GHC��������������������������������������������������������������������������������������������������������������� 172
Strictness Annotations�������������������������������������������������������������������������������������������������������������� 177
Summary���������������������������������������������������������������������������������������������������������������������������������� 180

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�������������������������������������������������������������������������������������������������������������������������� 211
Monad Laws������������������������������������������������������������������������������������������������������������������������ 215
Different Sorts of State������������������������������������������������������������������������������������������������������������� 217
State and Lenses����������������������������������������������������������������������������������������������������������������� 217
Reader, Writer, and RWS������������������������������������������������������������������������������������������������������ 219
Mutable References with ST����������������������������������������������������������������������������������������������� 224
Summary���������������������������������������������������������������������������������������������������������������������������������� 226

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 Rule Learning�������������������������������������������������������������������������������������������������������� 237
Flattening Values into Transactions������������������������������������������������������������������������������������� 238
The Apriori Algorithm����������������������������������������������������������������������������������������������������������� 241
Search Problems����������������������������������������������������������������������������������������������������������������������� 246
Paths in a Graph������������������������������������������������������������������������������������������������������������������ 247
The Logic Monad����������������������������������������������������������������������������������������������������������������� 248
Monads and Lists, Redux���������������������������������������������������������������������������������������������������������� 251
Combining Values Under a Monad��������������������������������������������������������������������������������������� 252
Monad Comprehensions������������������������������������������������������������������������������������������������������ 255

viii
Table of Contents

Combining Monads������������������������������������������������������������������������������������������������������������������� 259


Monad Transformers������������������������������������������������������������������������������������������������������������ 260
Monad Classes�������������������������������������������������������������������������������������������������������������������� 264
Summary���������������������������������������������������������������������������������������������������������������������������������� 267

Chapter 8: Working in Several Cores�������������������������������������������������������������������� 269


Parallelism, Concurrency, and Distribution������������������������������������������������������������������������������� 269
The Par Monad�������������������������������������������������������������������������������������������������������������������������� 271
Futures�������������������������������������������������������������������������������������������������������������������������������� 272
Dataflow Parallelism with IVars������������������������������������������������������������������������������������������� 275
Parallelizing the Apriori Algorithm��������������������������������������������������������������������������������������� 277
Software Transactional Memory����������������������������������������������������������������������������������������������� 280
Concurrent Use of Resources���������������������������������������������������������������������������������������������� 280
Atomic Transactions������������������������������������������������������������������������������������������������������������ 283
Rolling Back Transactions���������������������������������������������������������������������������������������������������� 286
Producer-Consumer Queues����������������������������������������������������������������������������������������������������� 288
Single-Process Queues������������������������������������������������������������������������������������������������������� 289
Message Queues Using AMQP��������������������������������������������������������������������������������������������� 291
AMQP in Haskell������������������������������������������������������������������������������������������������������������������ 293
Summary���������������������������������������������������������������������������������������������������������������������������������� 297

Part III: Resource Handling���������������������������������������������������������������� 299


Chapter 9: Dealing with Files: IO and Conduit������������������������������������������������������ 301
Basic Input and Output�������������������������������������������������������������������������������������������������������������� 301
Randomness����������������������������������������������������������������������������������������������������������������������������� 307
Working with Files�������������������������������������������������������������������������������������������������������������������� 310
Reading and Writing������������������������������������������������������������������������������������������������������������ 310
Handling Files���������������������������������������������������������������������������������������������������������������������� 313
Error Handling��������������������������������������������������������������������������������������������������������������������������� 315
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������������������������������������������������������������������������������������������������ 334
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����������������������������������������������������������������������������������������������������� 348
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
Queries�������������������������������������������������������������������������������������������������������������������������������������� 395
Queries by Identifier or Uniqueness������������������������������������������������������������������������������������ 396

x
Table of Contents

Selecting Several Entities���������������������������������������������������������������������������������������������������� 397


SQL Queries with Esqueleto������������������������������������������������������������������������������������������������ 400
Insertions, Updates, and Deletions�������������������������������������������������������������������������������������������� 404
Summary���������������������������������������������������������������������������������������������������������������������������������� 408

Chapter 12: Web Applications������������������������������������������������������������������������������ 409


Haskell Web Ecosystem������������������������������������������������������������������������������������������������������������ 409
Web Applications����������������������������������������������������������������������������������������������������������������� 410
Compilation to JavaScript���������������������������������������������������������������������������������������������������� 412
RESTful Structure���������������������������������������������������������������������������������������������������������������������� 413
Back End with WAI�������������������������������������������������������������������������������������������������������������������� 414
Simple Skeleton������������������������������������������������������������������������������������������������������������������ 414
Showing Products from the Database��������������������������������������������������������������������������������� 417
Inserting New Products Using Forms���������������������������������������������������������������������������������� 422
Front End with Elm�������������������������������������������������������������������������������������������������������������������� 427
Retrieving Products������������������������������������������������������������������������������������������������������������� 432
Summary���������������������������������������������������������������������������������������������������������������������������������� 436

Part IV: Domain-Specific Languages������������������������������������������������� 437


Chapter 13: Strong Types for Describing Offers��������������������������������������������������� 439
Domain-Specific Languages����������������������������������������������������������������������������������������������������� 439
Embedding Your Language in Haskell��������������������������������������������������������������������������������� 440
The Offers Language����������������������������������������������������������������������������������������������������������������� 443
Adding Safety to the Expression Language������������������������������������������������������������������������� 446
Type-Level Programming���������������������������������������������������������������������������������������������������������� 450
Two Styles of Programming������������������������������������������������������������������������������������������������� 451
Representing Natural Numbers������������������������������������������������������������������������������������������� 452
Functional Dependencies���������������������������������������������������������������������������������������������������������� 454
Categories of Products with FDs����������������������������������������������������������������������������������������� 454
Vectors Using FDs���������������������������������������������������������������������������������������������������������������� 458
Enforcing the Presents Rule with FDs��������������������������������������������������������������������������������� 461
Type Families���������������������������������������������������������������������������������������������������������������������������� 463

xi
Table of Contents

Vectors Using TFs���������������������������������������������������������������������������������������������������������������� 464


Enforcing the Presents Rule with TFs���������������������������������������������������������������������������������� 465
Categories of Products with TFs������������������������������������������������������������������������������������������ 466
Data Type Promotion and Singletons���������������������������������������������������������������������������������������� 472
A Further Refinement to the Presents Rule������������������������������������������������������������������������� 472
Cooking with Singletons������������������������������������������������������������������������������������������������������ 474
Enforcing the Duration Rule������������������������������������������������������������������������������������������������ 476
Summary���������������������������������������������������������������������������������������������������������������������������������� 482

Chapter 14: Interpreting Offers with Attributes���������������������������������������������������� 483


Interpretations and Attribute Grammars����������������������������������������������������������������������������������� 484
A Simple Interpretation�������������������������������������������������������������������������������������������������������� 484
Introducing Attribute Grammars������������������������������������������������������������������������������������������ 484
Your First Attribute Grammar���������������������������������������������������������������������������������������������������� 487
Synthesizing the Result������������������������������������������������������������������������������������������������������� 488
Integrating UUAGC in Your Package������������������������������������������������������������������������������������� 490
Executing the Attribute Grammar���������������������������������������������������������������������������������������� 492
Expression Interpretation���������������������������������������������������������������������������������������������������������� 493
Using an Attribute Grammar������������������������������������������������������������������������������������������������ 494
Precomputing Some Values������������������������������������������������������������������������������������������������� 497
A Different (Monadic) View�������������������������������������������������������������������������������������������������� 499
Offer Interpretations������������������������������������������������������������������������������������������������������������������ 501
Checking the Presents Rule������������������������������������������������������������������������������������������������ 501
Showing an HTML Description�������������������������������������������������������������������������������������������� 503
Programming with Data Types�������������������������������������������������������������������������������������������������� 509
Origami Programming over Any Data Type�������������������������������������������������������������������������� 509
Data Type–Generic Programming���������������������������������������������������������������������������������������� 512
Summary���������������������������������������������������������������������������������������������������������������������������������� 516

xii
Table of Contents

Part V: Engineering the Store������������������������������������������������������������ 519


Chapter 15: Documenting, Testing, and Verifying������������������������������������������������� 521
Documenting Binary Trees with Haddock��������������������������������������������������������������������������������� 522
Unit Testing with HUnit�������������������������������������������������������������������������������������������������������������� 526
Declaring Tests in Cabal������������������������������������������������������������������������������������������������������ 527
Writing Unit Tests����������������������������������������������������������������������������������������������������������������� 528
Randomized Testing with QuickCheck�������������������������������������������������������������������������������������� 532
Testing List Properties��������������������������������������������������������������������������������������������������������� 533
Testing Binary Tree Properties��������������������������������������������������������������������������������������������� 534
Formal Verification with LiquidHaskell�������������������������������������������������������������������������������������� 537
Describing Binary Search Trees������������������������������������������������������������������������������������������� 541
Summary���������������������������������������������������������������������������������������������������������������������������������� 543

Chapter 16: Architecting Your Application����������������������������������������������������������� 545


Tools������������������������������������������������������������������������������������������������������������������������������������������ 545
Project and Dependency Management�������������������������������������������������������������������������������� 546
Code Style��������������������������������������������������������������������������������������������������������������������������� 546
Documentation�������������������������������������������������������������������������������������������������������������������� 547
Test and Verification������������������������������������������������������������������������������������������������������������ 547
Benchmarking��������������������������������������������������������������������������������������������������������������������� 547
Profiling������������������������������������������������������������������������������������������������������������������������������� 548
Coverage����������������������������������������������������������������������������������������������������������������������������� 548
Remote Monitoring�������������������������������������������������������������������������������������������������������������� 548
Design Patterns and Functional Programming������������������������������������������������������������������������� 549
Medium-Level Guidelines��������������������������������������������������������������������������������������������������������� 551
Use Higher-Order Combinators�������������������������������������������������������������������������������������������� 551
Refactor������������������������������������������������������������������������������������������������������������������������������� 551
Use Type Classes Wisely������������������������������������������������������������������������������������������������������ 551
Enforce Invariants via the Type System������������������������������������������������������������������������������� 552
Stay (As) Pure and Polymorphic (As Possible)��������������������������������������������������������������������� 552
Patterns with Monads��������������������������������������������������������������������������������������������������������������� 553

xiii
Table of Contents

Summary of Monads����������������������������������������������������������������������������������������������������������� 553


Restrictive Monad Classes�������������������������������������������������������������������������������������������������� 560
Roll Your Own Monad���������������������������������������������������������������������������������������������������������� 561
Summary���������������������������������������������������������������������������������������������������������������������������������� 564

Chapter 17: Looking Further��������������������������������������������������������������������������������� 565


Projects������������������������������������������������������������������������������������������������������������������������������������� 565
Data Mining Library������������������������������������������������������������������������������������������������������������� 566
Store Network Client����������������������������������������������������������������������������������������������������������� 568
Administration Interface and Tetris������������������������������������������������������������������������������������� 569
Additional Haskell Resources���������������������������������������������������������������������������������������������������� 570
Other Functional Languages������������������������������������������������������������������������������������������������ 571

Appendix A: Time Travelling with Haskell������������������������������������������������������������ 573

Index��������������������������������������������������������������������������������������������������������������������� 577

xiv
About the Author
Alejandro Serrano Mena has more than a decade of
experience as a developer, trainer, and researcher in
functional programming, with an emphasis in Haskell and
related languages. He holds a PhD from Utrecht University
on the topic of error message customization in compilers.
Most of his work relates to tools helping programmers
to write more correct and reliable code, including static
analyzers and formal verification tools.
Alejandro is an active member of the community. He
maintains several open source projects, has written three books on different levels about
Haskell, and co-hosts a podcast on the same topics. He also gives talks and conducts
workshops at different conferences, spreading the love and the techniques of functional
programming.

xv
Another Random Scribd Document
with Unrelated Content
Virginia looked, pleadingly, in the face of Kate, and wondered to see
her brown cheek pale and her great eyes fill with tears.
“Oh, you do not know what you ask!” cried Kate, in agony, “and I
can not tell you.”
Virginia heard the strange words in amazement.
“Can you not be my sister?”
“No, no, it is impossible,” Kate murmured, sadly.
“Impossible, why?”
“Because—”
The wild war-whoop of the Shawnees, pealing forth on the still
morning air, and ringing in the ears of the three like a signal of
doom, cut short Kate’s words.
Then the door yielded to a heavy blow, and a score of dark forms
rushed into the room.
CHAPTER XXXIX.
THE TOTEM OF THE RENEGADE.

A single glance at the dark forms that filled the doorway, and the
hearts of the three sunk within them.
They were prisoners to the Shawnees!
At the head of the painted warriors was Simon Girty, the renegade.
Girty’s eyes lit up with fiend-like joy as he gazed upon his captives.
“A keen she-devil you are, to snatch the game out of my hands; but
did you think that you could escape from me so easily?” he cried,
addressing Kate.
The warm blood flushed the face of the “Queen,” as she listened to
the insolent words of the white Indian.
“You are in my power; no human force can snatch you from me,” he
continued, exultingly. “A nice trick it was, to pretend to watch my
prisoner for me, and then aid her to escape in the darkness! But I
tracked you, though, cunning as you are. A fit daughter of a worthy
father; but, maybe, my turn will come now, Chiefs,” and he turned to
the warriors that filled the doorway, “which of you want this dainty
brown maid for a squaw? I’ll give her to one, for her fate is in my
hands now.”
All the fire in Kate’s nature shone in the lurid flash of her dark eyes.
“Take care, Simon Girty!” she cried, in anger. “If my father is not
man enough to protect me from insult, my rifle will.”
“Your father is dead, girl, or mighty near it,” returned Girty,
scornfully. “When I discovered the trick that you and he played upon
me, I sunk my tomahawk in his skull and let out his fool’s brains.”
“My father slain!” cried Kate, in horror.
“I reckon that there isn’t much life left in him by this time. He dared
to cross my will, the hound that he was, and I struck him to his
death,” said Girty, fiercely.
Kate felt that she was indeed at Girty’s mercy.
“And for you, my pretty white bird,” and the renegade turned to
Virginia as he spoke, “did you fancy that you could escape the fate
that I marked out for you? You will learn in time that my blows
seldom fail.”
“Oh, have you no mercy!” cried Virginia, in despair.
“What mercy did your father have when his lashes tore my back,
long years ago?” demanded the renegade, fiercely. “The mercy that
he showed to me I will show to him and his. I’ll tear his heart as his
punishment tore my flesh. When he learns your shameful fate, then,
and not till then, will the debt of vengeance be canceled. How he will
curse his evil fortune when he learns that his dainty daughter—the
apple of his eye, the pride of his old age—is the victim of the
renegade, Simon Girty!” and then he laughed loud and long.
“Accursed villain!” cried Winthrop, suddenly, unable to restrain his
fury; and quick as thought, he flung himself upon the renegade,
regardless of the overpowering number of foes that surrounded him.
With a single heavy blow between the eyes, he beat the renegade,
like a log, to the ground; but ere he could pursue his advantage
further, the Shawnee warriors dashed themselves upon him. Ten to
one, Winthrop was speedily overcome and securely bound.
The renegade rose to his feet, his eyes gleaming like a demon’s, and
a livid mark upon his face, where the knuckles of the young man
had bruised the skin.
“You shall pay dearly for that blow!” Girty cried, between his
clenched teeth. “You shall die at the torture-stake, a thousand
deaths all in one. The tomahawks of the Indians will cut your flesh
from your bones, even while you are a living man. You will cry aloud
for death to come to end your misery. And in your last moments the
thought will come that this fair girl—whom I guess you love—will be
wholly in my power—a helpless victim to my caprices. And as you
die in lingering torments, I will stand by your side and taunt you till
death releases you from my power.”
Words can but feebly describe the waked wrath of the renegade.
Winthrop faced him undauntedly.
“It suits your cowardly nature better to taunt a helpless prisoner
than to face a free man. I do love this girl, and the thought that she
is helpless in your power, demon that you are, gives me greater pain
than can all the fire and torture of the red devils with whom you
claim kindred. I am your captive. Look well to me; see that I do not
escape from you, for it would cost you your life if I should ever again
regain my freedom.”
Every muscle in the young man’s form swelled with indignation as he
spoke.
“When you cease to be my captive, death will claim you,” replied
Girty, grimly.
Kate looked around her. She saw no avenue of escape. She felt that
they were hopelessly lost.
“Come,” said Girty; “but first bind the wrists of these two squaws.”
The Indians obeyed his order.
“Now for your future home, the Shawnee village!” Girty cried, in
triumph.
The Indians and their prisoners, led by the renegade, passed
through the door of the cabin and stood within the little clearing that
surrounded the house.
Then forth from the timber came the Shawnee brave, Noc-a-tah.
He came straight to Girty.
“Well, chief, what is it?” asked the renegade. He conjectured from
the Indian’s manner that he was the bearer of some important
tidings.
“Your white brother has gone to the land of shadows—he sends this
totem to you.” Then the Indian drew from his pocket the piece of
birch bark whereon Kendrick had, with his blood and the pointed
twig, traced his dying words.
“Dead, eh?” said Girty, with a sneer. “A totem to me? What can it
be?”
Then the renegade took the piece of bark and endeavored to read
the lines.
Rudely were the letters formed, for Dave Kendrick could boast of but
little scholarship.
The renegade puzzled over the writing. Suddenly the meaning
flashed upon him. A gleam of fierce joy swept over his dark face.
“By all the fiends, this is double vengeance!” he cried in glee. “Chief,
in Chillicothe, thou shalt have the best scalping-knife that I own, in
payment for this precious totem.”
Noc-a-tah gravely nodded, and then disappeared within the thicket.
Girty turned to where the two girls stood, side by side.
The maidens wondered at his searching look.
“What a blind idiot I have been not to have noticed it before,” he
muttered, “and yet I remember, now, the face of the girl did look
familiar to me when I first saw her in the Shawnee village. To think
of my vengeance slipping through my fingers, and then, after long
years, being put again within my hands! There’s fate in this. And
Kendrick, too—he thought, by this dying declaration, to strike a blow
at me, even from the grave. He thought both the girls were safely
out of my hands. He little dreamed when I should read his ‘totem’—
as the savage termed it—that the two he referred to in it would be
helpless prisoners in my power. Could he have foreseen that, he
would have cut off his hand rather than divulge to me what he has
here written.”
Then the renegade laughed long and silently. His captives wondered
at his glee.
“You risked your life to save this girl; why did you do it?” he asked of
Kate, suddenly.
“Because she was helpless in the power of a cruel monster. My heart
told me to save her, even at the risk of my own life,” replied Kate,
promptly.
“And you, girl—are you not grateful to this maiden, who has tried so
hard to save you from me?” he said to Virginia.
“Yes, I am very grateful,” replied the girl, wondering at the question.
“Their hearts don’t tell ’em,” muttered the renegade. “The old adage
is a fable; blood is not thicker than water. Virginia, years ago I stole
your eldest sister, and left her to perish in the forest. This was the
first blow that I aimed at your father. Now see how strangely fate
sometimes disposes of things in this world. The child that I left to
die did not die, but was saved, and has grown to womanhood, and I
all the time thinking her dead. Girls, can’t you guess the truth? The
man that saved and reared the child was Dave Kendrick, the
renegade!”
The truth flashed upon the maidens in an instant.
“Sister!” cried Virginia, warmly; but the bonds upon their wrists
forbade further greeting.
“Yes, she is your sister. Kate, you are Augusta Treveling, the eldest
daughter of the old General,” said Girty, and a triumphant smile was
upon his face.
The smile made the two girls tremble.
“The hound that I gave to the worms never told the secret to me,
but, dying, he wrote it here on this piece of bark. This was his
vengeance,” and Girty laughed loudly. “It will be pleasant news to
the old General, your father, when he hears that both of his
daughters are living, and both are in my power.”
“Oh, man, have you no mercy?” plead Kate.
“Mercy?” cried the renegade, fiercely. “Ask it of the hungry wolf, the
angry bear, or the red savage, when his knife is raised to slay!
Expect mercy from all these, but expect none from the man whose
skin is white but whose heart is red. Come; in Chillicothe you will
meet your fate.”
A broad sheet of flame, springing from the woods to the north of the
little clearing, followed by the sharp report of a dozen rifles,
answered the boast of the renegade.
Of the ten savages who had followed Girty’s lead, seven lay
wounded or dead upon the earth.
From the timber came the ringing shout of the borderers, and a
score or more of the settlers, headed by General Treveling and stout
Jake Jackson, came with a rush into the clearing.
Girty, though badly wounded, and the unhurt savages, had fled at
once.
Jake and fully one-half of the borderers followed in pursuit.
The captives were speedily released from their bonds.
“Let me give thanks to that Heaven that in its bounty has seen fit to
give me back both my daughters to gladden the last years of an old
man’s life!” cried Treveling, in joy, as he folded his children to his
heart.
The timely arrival of the settlers was easily explained. Noc-a-tah, the
Shawnee chief, had faithfully kept the promise made to the dying
renegade, and had first sought Point Pleasant and given the “totem”
into the hands of the General.
The father’s joy on learning that his eldest daughter lived can easily
be imagined.
Jackson, who had seen the Indian depart, instantly counseled that
he should be tracked, that the whereabouts of the rest of the
Shawnees might be discovered.
The advice of the stout Indian-fighter had been followed, and the
happy result was, the rescuing of Girty’s victims.
Well might the aged father lift up his voice in joy.
CHAPTER XL.
THE WHITE DOG AND THE WOLF DEMON.

“He’s a plucky young cuss, ain’t he?” said Boone, in a whisper to


Kenton, when he heard the bold defiance of the Indian warrior.
“The Wolf Demon will make mince-meat out of him ef he puts his
claws onto him,” replied Kenton, in the same cautious whisper that
Boone had used.
“I wonder if the spook will come?” said Boone.
“I reckon not; them things never come when they’re expected. They
alers take their own time,” returned Kenton.
“How easy we could ‘rub’ him out now, eh?” Boone observed,
suggestively.
“I’m afeard it would bring a hull grist of his relations down on top of
us, thick as skeeters in a swamp,” said Kenton.
“That’s so.”
A movement on the part of the Indian put a stop to their
conversation, and eagerly they bent their eyes upon him.
After pronouncing his war-like defiance, the warrior, with his
tomahawk in one hand and his keen-edged scalping-knife in the
other, remained motionless as a bronze statue.
Full five minutes he waited.
His eager eye, quick and piercing as the eye of a hawk, surveyed the
forest before him.
He heard each rustling leaf that stirred in obedience to the soft
night-wind’s commands; the noise of the pinions of the owl, winging
its nocturnal flight through the dim aisles of the great, green wood;
the cry of the tree-toad; the chirrup of the cricket, deep down in the
earth. But, none of these stirred the senses of the Indian. He knew
the voices of the night full well, for he was a child of the forest and
had slept many an hour beneath the shadows of the spreading
boughs.
He listened for a sound that he heard not—the tread of the great
gray wolf, who wore the face of a man.
Impatient, the warrior uttered a guttural exclamation.
Again he addressed the silence and the gloom, called for the dread
being to appear, at whose approach all living things of the earth or
air fled.
“The warrior is weary of waiting. If the Wolf Demon is in the thicket
let him come forth. The White Dog will strip off his hide, or else the
Wolf Demon shall take his scalp and mark the totem of the Red
Arrow on his breast.”
But the silence and the gloom replied not to the bold defiance.
After a pause of a few moments the warrior uttered a contemptuous
exclamation.
“The Wolf Demon should wear the skin of the muskrat; he skulks in
the dark and fears to meet his foe face to face.”
The chief turned upon his heel and thrust his scalping-knife into his
girdle as if to depart.
One single step he made, and then a sound fell upon his ears that
made him pause; made him draw the keen-edged knife again from
his belt; made him prepare for battle.
The quick ear of the Indian—trained from infancy to note the noises
of the forest, the plain and river—heard a stealthy step prowling
through the thicket.
The noise came from behind him. Quick as thought the warrior
turned and faced the point from whence the noise proceeded.
No form stepped from the timber into the little glade, whereon the
soft moonbeams fell, but the Indian still heard the sound of the
stealthy steps.
The steps seemed to come no nearer, and yet the sound grew no
fainter.
Whoever was within the wood was circling around the Indian as if to
attack him in the rear, and by surprise.
The chief guessed the truth, and as the unknown foe moved, he
moved. Slowly he turned, keeping his face always in the direction
from whence came the sound of the steps.
The two borderers, concealed in the thicket, watched the
movements of the Indian with astonishment.
When he assumed the attitude of defense and drew again the knife,
Boone nudged Kenton.
“He hears something,” he whispered.
“’Pears like it,” Kenton replied.
And as they watched the Shawnee, they, too, heard the sound of
stealthy steps approaching them.
They noted that, as the steps approached, the face of the chief
seemed to follow the direction of the steps.
At once the Indian-fighters guessed the truth; guessed that the
terrible Wolf Demon, lurking in the thicket, was circling around the
chief, eager to spring upon him unawares.
The stealthy steps came nearer and nearer to the concealed men.
Boone, stretched out so near to Kenton that he touched him, felt
that the stout borderer was trembling like an aspen leaf; and, to
speak the truth, the blood in Boone’s veins was running cold with
horror.
There, not a dozen paces from them, within the thicket, they saw
the terrible form of the Wolf Demon.
With stealthy step he moved through the wood, his eyes glaring, like
coals of fire, upon the Indian warrior. In his paw he carried the fatal
tomahawk that had brained so many Shawnee chieftains.
The terrible form was moving in a circle around the warrior. But the
Indian was on his guard, and, guided by the sound of the stealthy
steps, kept his front always to his foe.
The Wolf Demon completed the circle, and then, as if fully satisfied
that he could not take the warrior by surprise, came slowly from the
thicket and stood within the open space; not, though, in the soft
light of the moonbeams, but half hid by the shadows thrown by the
forest monarchs that hemmed in the little glade.
The keen eyes of the Indian detected the appearance of the terrible
form.
The light of fierce determination shone upon the face of the
Shawnee warrior, and firmly he grasped his weapons and waited for
the onset of the foe.
Boone and Kenton, in breathless suspense, watched from their leafy
covert, eager to see the issue of the contest that was, apparently, so
near at hand.
A few seconds only the Wolf Demon paused within the friendly
shadows of the wood; then, with the swiftness of forked lightning,
he leaped upon the Shawnee warrior.
Bravely the Indian met the assault. With his tomahawk he parried
the blow aimed at his head, and, at the same moment, drove his
long knife, up to its haft, in the side of the phantom foe; but, the
glittering blade met no flesh in its passage, and not a single drop of
blood dimmed the brightness of the steel.
The thrust of the Shawnee chieftain cost him dear, for, ere he could
withdraw his knife again, the tomahawk of the Wolf Demon
descended upon his head. By a quick motion of his own ax he partly
parried the blow, but the force of the stroke bore him over backward
to the earth.
With a howl of triumph the Wolf Demon planted his foot upon the
warrior’s breast, and the glittering tomahawk gleamed before his
eyes, raised to give the death-blow.
The warrior felt that he was lost.
The death-note of his nation broke from his lips.
Then, forth from the timber, from the direction in which the Indian
village lay, came the Shawnee girl, Le-a-pah.
She had arrived upon the scene of conflict just in time to witness the
discomfiture of her lover.
With outstretched arms and a cry of horror—regardless of her own
life—she rushed forward to save her lover from the edge of the fatal
tomahawk, which was raised to drink his blood.
The paw of the Wolf Demon which clutched the tomahawk remained
poised in the air as the girl advanced. The blow descended not upon
the unprotected head of the prostrate man.
The phantom form, motionless as one of the forest oaks, glared
upon the Indian girl with its eyes of fire as if struck dumb with
horror.
It was a startling tableau.
The scouts looked on with awe-struck eyes. They expect each
instant to see the tomahawk descend, and the Indian girl fall lifeless
at the blow.
Steadily for a few moments the Demon form glared at the girl, and
then, taking its foot from the breast of the down-trodden chief, it
retreated backward with slow steps, toward the forest, still, however,
keeping its eyes upon the face of the girl as though under the
influence of some terrible enchantment.
The Indian chief, hardly able to realize that he was saved from the
death that but a moment before seemed so certain, made no effort
to rise, but appeared transfixed with horror.
The Wolf Demon gained the shadow of the thicket, and then—as if
the spell that had bound him had been broken—with a terrible cry,
that rung through the forest like the wail of a lost soul, doomed
forever to eternal fires, he vanished amid the darkness.
The cry of the Wolf Demon froze the blood of his hearers with
horror.
The girl, with a sob of terror, sunk down by the side of the young
chieftain.
Rising, the Shawnee warrior tenderly lifted the light form of Le-a-pah
from the greensward.
“Light of my heart, thou hast saved the life of the red chief!” cried
the warrior.
“Le-a-pah could not bear the thought that her lover should seek the
terrible Wolf Demon in the wood; she followed in his track to urge
him to return,” said the maiden.
“The White Dog has tried to win Le-a-pah. If he has failed to kill the
Wolf Demon, it is because the Great Spirit wills that he shall not die
by the hand of a red-man.”
“Let us seek my father. I will beg him to release you from the cruel
task.”
Then the chief and the maiden left the glade.
After a few minutes Boone and Kenton came from their hiding-place.
“Now, let’s look for Lark,” said Boone. “I’ve a thought that he has
met this terrible Wolf Demon when he was hyer afore, and that the
sight has made him mad.”
The two left the glade, and to their surprise found they stood before
the hollow oak which had served them as a rendezvous when in the
wood before.
At the foot of the oak they found Lark’s cap. As Boone picked it up, it
felt moist.
He looked at his hand.
It was stained with blood.
“By heaven!” he cried, in horror. “Lark has been killed, and perhaps
by the Wolf Demon!”
CHAPTER XLI.
THE FIGHT UNTO THE DEATH.

The two scouts looked upon the blood-stained cap with horror.
“The blood is fresh, too!” cried Boone. “Lark must have been killed
by this monster immediately after we missed him in the thicket.”
“It looks like it,” said Kenton, solemnly.
“Let us look for the body.”
But as they were about to commence their search, the sound of
footfalls approaching through the wood fell upon their ears.
“Hush!” cried Boone, grasping Kenton by the arm as he spoke; “do
you hear that?”
“It’s some one coming through the wood.”
“Yes, and hyer all comers are enemies and not friends; let’s to
cover,” said Boone.
A second after the two woodmen were snugly concealed in the
bushes.
The steps came nearer and nearer, and then, through the gloom of
the night, the watching eyes of the two saw the fearful form of the
terrible Wolf Demon approaching.
He walked not now with stealthy tread but his step was heavy and
slow. His head was bent down, low upon his breast. Slowly he came
on, passed by the ambush of the scouts, then crossed the moonlit
glade and entered the thicket on the opposite side. He was bending
his steps in the direction of the Indian village of Chillicothe.
Hardly had the awful form disappeared within the gloom of the
forest when Boone grasped Kenton nervously by the shoulder.
“Kenton,” he said, in a hoarse whisper, “let us not search for the
body of our friend, whom this awful thing has killed, but revenge his
death.”
“I’m with you, tooth and nail,” replied Kenton, firmly.
“Let’s follow this thing then.”
“Go it,” said Kenton, tersely.
Then the woodmen, with caution, followed in the path of the Wolf
Demon.
The Demon proceeded direct to the Indian village.
The woodmen were guided in their course by the noise of his
footsteps.
Suddenly the sound of the steps ceased.
Boone and Kenton crept forward with increased caution.
A few rods on and they found themselves on the edge of the timber,
and in full view of the Indian village.
The Wolf Demon was not to be seen!
The scouts then guessed the reason why the sounds of the Wolf
Demon’s tread had ceased so suddenly. The Demon had entered the
village in search of prey.
The path that the two had followed entered the village close by the
river’s bank.
It was plain to Boone that the Wolf Demon had selected the same
road into the Indian village that he, Boone, had taken in escaping
from it.
“We’re treed,” said Boone, as they reached the edge of the timber
and perceived that they could proceed no further in their pursuit
without danger of their being discovered by the red-skins.
“A full stop hyer,” said Boone, thoughtfully.
“Yes, it ’pears like it,” Kenton replied.
“S’pose we wait hyer for the varmint? Ef he went into the village this
way, it’s likely that he’ll come out the same path.”
“That’s true.”
“Yes, as preachin’. I don’t know as we kin damage the critter,” said
Boone, thoughtfully. “We hain’t got no silver bullets, and I’ve heerd
say that it takes a silver bullet to stop a spook.”
“We kin try,” said Kenton, decidedly.
“Right again, by hookey! Give us your paw, Sim; we’ll stick by each
other in this.”
“Yes, to death,” answered Kenton.
A firm grip of hands sealed the compact.
Then the two again concealed themselves in the bushes.
They watched and they waited.

In the Indian village, Ke-ne-ha-ha, the great Shawnee chieftain, sat


in the gloom of his wigwam.
The little fire that burned in the center of the lodge cast a baleful
light over the dusky face of the warrior.
Dark and full of sorrow were the thoughts of the chieftain.
He saw again the death-scene of the Red Arrow; heard her shriek
for mercy, and then beheld the warm life-blood gushing, free, from
her young veins. Amid the smoke and flames, she died. Like the
Roman father, he had given to the death his own flesh and blood.
And that deed had brought upon his nation the terrible scourge of
the Wolf Demon.
Well might the brow of Ke-ne-ha-ha look dark as the thunder-cloud
when he thought of the past. And in the future he saw no ray of
light. He had little hope that the White Dog would succeed in his
mission and kill the terrible foe.
As he was brooding over these gloomy thoughts, his daughter, Le-a-
pah, entered the wigwam.
“May the White Dog speak with the chief?” the girl asked.
“Let the brave enter,” Ke-ne-ha-ha replied. A gleam of light flashed
over his clouded face. Why should the young warrior seek him, save
to tell of the death of the Wolf Demon?
A second more and the warrior stood before him. The girl remained,
discreetly, at the door of the lodge.
“Well?” questioned the chief.
“The White Dog sought the Wolf Demon in the forest, fought him
hand to hand, but the Shawnee brave fell beneath his foot; the
tomahawk was raised to strike, when Le-a-pah bounded from the
wood and the Wolf Demon held his arm and fled from her like the
night flies from the dawn.”
Ke-ne-ha-ha listened, in amazement.
“The warrior has failed,” he said, slowly.
“Manitou did not will that he should kill the Wolf Demon,” replied the
young brave.
“The brave has tried, and the Shawnee chief will keep his word. Le-
a-pah!”
The maiden came at his call.
The chief gave her to the embrace of the young warrior.
“You are both my children—go.” But no gleam of joy lighted up Ke-
ne-ha-ha’s stern face as he gave his daughter into the arms of her
lover. The living Wolf Demon cast a mantle of gloom over his brain.
The brave and the girl withdrew from the lodge. The manner of the
chieftain forbade further words.
Left alone, Ke-ne-ha-ha strode up and down the narrow confines of
the wigwam in sullen thought.
“Oh, that my life might save my people from this terrible scourge!”
he murmured, with clenched teeth. “For the two lives, he has taken
twelve. How many more of my nation must fall by the tomahawk of
the Wolf Demon ere his taste for Shawnee blood will be satisfied?”
“One!” responded a deep voice.
Ke-ne-ha-ha turned, his blood chilled to ice with horror.
His eyes looked upon the terrible form of the Wolf Demon standing
in the doorway of the wigwam. In the hand of the Demon shone the
deadly tomahawk.
Ke-ne-ha-ha gazed with staring eyes upon the terrible figure.
“Let the chief prepare to die. He is the last Shawnee that will feel the
edge of the tomahawk of the avenger,” cried the deep voice.
With an effort, Ke-ne-ha-ha roused himself from the spell of terror
that the appearance of the dreaded Wolf Demon had cast around
him.
With a sudden bound, he seized his tomahawk, that had been
carelessly cast upon the floor of the wigwam.
The Wolf Demon made no effort to prevent the chief from
possessing himself of the weapon.
Tomahawk in hand, the foes faced each other.
Slowly they moved around the narrow circle of the wigwam,
watching each other with wary eyes, each seeking an unguarded
opening for an attack.
Thrice they made the circle of the lodge, the little fire, with its
glimmering light, revealing their movements to each other.
Then with a spring, like unto the panther’s in quickness, and in
force, the Wolf Demon leaped upon the Shawnee chief.
Ke-ne-ha-ha did not seek to parry the attack, but nimbly he evaded
it by springing to one side.
The tomahawk of the Wolf Demon spent its force upon the air; and
as he passed, the wily Indian dealt him a terrible stroke upon the
head, that cut in deep through the wolf-skin, and felled him heavily
to the earth.
A hoarse note of triumph came from the lips of the chief as he
beheld the downfall of his foe. But his joy was of short duration, for,
like the ancient god of the fable that gathered strength from being
cast to earth, the Wolf Demon rose to his feet. The shock of the fall
had torn the tomahawk from his hand, but he did not attempt to
recover the weapon.
With naked hands—weaponless—he faced the Shawnee chief. The
blood streaming down freely over his face—over the black and white
pigments with which it was painted in horrid fashion—made him look
like an evil spirit fresh from the fires below.
His eyes shot lurid flames as he glared upon the Shawnee warrior.
Ke-ne-ha-ha grasped his tomahawk with desperate energy and
waited for the attack of the unarmed foe.
The Shawnee chieftain did not have long to wait.
With the spring of a tiger the Wolf Demon leaped upon the Indian.
Desperately Ke-ne-ha-ha struck at him with the tomahawk, but the
Wolf Demon warded off the blows with his arm, and despite the
efforts of the chief to prevent it, he closed in with him.
Sinewy and supple was the Shawnee warrior, yet he was but as a
child in the powerful grasp of his terrible foe.
The Wolf Demon held him in a grip of iron. His arms, linked round
the Indian like bands of steel, were crushing the life out of him little
by little.
Vainly Ke-ne-ha-ha struggled to free himself from the anaconda coil.
Like the serpent of far-off India, wreathing its huge length around its
prey, the Wolf Demon held the Shawnee chieftain in his grip.
The breath of the Indian came thick and hard.
Up and down in the narrow confines of the wigwam swayed the
contending foes, like two venomous snakes coiled together.
Exerting all his strength, the Indian tried to break the grasp of the
Wolf Demon. Vainly he struggled—vainly he tried. He felt that his
strength was going fast.
Tight and tighter grew the grip of steel.
The Indian turned black in the face. The blood gushed from his
mouth. He ceased to struggle. The grip relaxed and Ke-ne-ha-ha fell
to the ground, dead.
CHAPTER XLII.
THE LAST OF THE DEMON.

A look of triumph swept over the blood-stained face of the Wolf


Demon as he looked upon the lifeless form of the Shawnee warrior.
From the cut in the head of the Wolf the blood was slowly trickling,
but he did not seem to mind the hurt.
With a hoarse cry of joy he knelt by the side of the man whom he
had strangled to death with his powerful arms.
He tore the hunting-shirt from the breast of the dead chieftain; then
he drew the dead man’s knife from his girdle.
Three rapid dashes and the Red Arrow, graven in the flesh, was
blazoned on the breast of the Shawnee warrior.
“Inhuman dog, more like the wolf in heart than I, thus do I mark
you,” the Wolf Demon cried in a voice hoarse with passion. “Eleven
red demons slew the Red Arrow, eleven Shawnee warriors have I
slain. Not one of the murdering band has escaped my steel. She fell
in the blazing cabin amid the great green wood, near where the
Muskingum waters laugh and play. The assassins have fallen in the
glade and in the woodland, by the banks of the Scioto and the Ohio,
in the paths of the Shawnee village and by the lodge-fires of the
Chillicothe. I have struck them down by night and by day. And on
each breast, in memory of the Indian maid that I once loved so well,
have I stamped the Red Arrow. Now, at last, the chief of the red
band of slayers has felt the edge of the scalping-knife. My work is
done—my mission ended, and now, death, take me for thine own.”
The Wolf Demon rose to his feet and glared wildly around him. His
eyes were starting from their sockets and gleamed like balls of fire.
“What is this I see?” he cried, suddenly; “a river of blood! It is the
blood of the red warriors that have fallen by my hand, and she the
loved and lost is in its center. She beckons me to her. I see her as
plainly as I did an hour ago when she sprung from the earth in the
woodland glade by the hollow oak, to save the young Indian warrior
from my vengeance. I know that he was not one of the assassin
band that took thy life, but in his veins ran the blood of the accursed
Shawnees, and I had doomed him to the death. But I spared him.
Did you not come from thy spirit home among the blest and lift up
thy hand to stay my arm? Go on, I’ll follow thee! Death is near. It is
welcome, for it brings me to thee, my love. I hear the song of angels
in mine ears! I am coming.”
Slowly, with his eyes fixed vacantly on the air, the Wolf Demon came
from the lodge, descended the bank, and hid by it from sight, left
the Shawnee village.
Boone and Kenton from their ambush perceived him approach.
Boone touched Kenton on the arm as if to call his attention, but
Kenton had already perceived the terrible figure.
“Shall we fire at him?” questioned Kenton, in a whisper, and the
usually firm hand of the borderer trembled as he fumbled with the
lock of his gun.
“No, no!” cried Boone, quickly, and in a cautious whisper; “the report
would bring the hull of the Shawnee village down upon as, jist like
stirring up a nest of hornets.”
“What shall we do, then?”
“We’ll follow and attack him in the forest,” answered Boone.
The Wolf Demon came slowly on, his eyes staring full upon the air
before him. He passed by the ambush of the two woodmen and
entered the thicket.
As he passed, the two noted the signs of a conflict so apparent upon
him.
“Jist look at his face! it’s kivered all over with blood!” exclaimed
Boone, in wonder.
“He’s fixed another Shawnee, I reckon,” said Kenton, seriously.
“Sim, it’s a terrible thing to attack this awful critter,” said Boone, with
a grave look upon his honest face.
“But the death of poor Lark—”
“Must be avenged!” exclaimed the old hunter, compressing his lips
together, firmly.
“That’s so, said Kenton, with a pale face and a throbbing heart, yet
with undaunted courage.
“I didn’t see as he had any we’pons, but ef he’s the devil, he don’t
need any. Come on, we’ll give him a tussle, anyway. Lord, I wish I
could remember a prayer or two,” said Boone, seriously.
Then with cautious steps they followed on the trail of the Wolf
Demon.
The singular being pursued the same path returning that he had
taken when coming through the wood.
He moved so slow that the two in pursuit followed him without
difficulty.
Every now and then he halted for a moment and then again went
on.
His steps became irregular. The hunters, following close behind,
noticed that he was reeling like a drunken man.
From side to side he swayed as he made his way through the forest.
He reached the little glade by the side of which stood the hollow
oak.
“Let’s attack him in the glade!” cried Boone, as he and Kenton
reached the edge of the opening and beheld the Wolf Demon
standing motionless, as if irresolute, in the center of it.
“Come on, then.”
Clubbing their rifles—they did not dare to fire for fear of the report
arousing the Indian village—the two scouts dashed into the opening.
Hearing the noise of their footsteps, the Wolf Demon turned,
extended his arms as if to stay their progress, and then, with a
heavy groan, fell sideways to the ground. The sudden shock burst
the wolf-head from its fastenings to the body, and it rolled away
from the prostrate figure.
The scouts halted in astonishment.
The wolf-head gone, the head of a man, covered with light,
clustering curls, was revealed to their gaze.
Quickly they knelt by the side of the Wolf Demon and wiped the
blood and war-paint from his face.
The superstitious fear of the woodmen was all gone now, for they
knew that it was a human form that lay extended on the earth
before them.
The terrible Wolf Demon was dying. The tomahawk of the Shawnee
had given him his death-wound. The strong limbs, once so powerful,
were now made feeble by the near approach of that terrible mystery
that human mind never yet has solved.
The two scouts lifted up the head of the dying man. His eyes opened
slowly and, with a vacant look, he gazed around him.
“Oh, what a terrible dream!” he murmured, faintly.
The woodmen bent their heads, eagerly, to listen.
“It seems as if I have waded through a river of blood—fresh, warm
blood, gushing, freely, from terrible wounds. I dreamed that I had
been changed into a wolf, a beast with a human soul, and in that
soul one thought only, vengeance on the Shawnee nation. In the
light and in the darkness I sought that vengeance. The red braves
fell around my path as the wheat falls around the reaper, yet I staid
not my hand, for the cry went up for blood, rivers of it. On each
victim I cut my mark, a Red Arrow, in remembrance of the wife that
the red demons tore from me a year ago by the Muskingum. I was
gifted with the cunning of a maniac, for at times I am mad. The
wound on my head, that I received from a falling rafter on that
fearful night when my wife was killed, affected my brain. In my
madness I must have dreamed all these terrible things. Dreamed
that I fashioned myself a wolf-skin like a wolf, and then struck down
my foes. A hollow oak in the forest was my home; there I concealed
my wolf-skin when my mad fit was over. Oh! it was a terrible dream.”
Boone and Kenton exchanged glances; they knew that the dream
was a reality.
Then the eyes of the stricken man, glaring around him, fell upon the
strange disguise that covered his person.
“What is this?” he cried, in horror; “the skin of a wolf! Then it is not
a dream! No, no, I see all clearly now; the near approach of death
has cleared my eyes unto the truth. In my madness I have been like
an avenging angel to the Shawnee nation. I see their tall forms
around me now—masculine warriors—the tomahawk cut is on their
skulls, and on their breast is graven in lines of warm blood the
emblem of vengeance, the Red Arrow!”
Exhausted by the outburst, his head sunk back upon the knee of
Boone.
“Heaven have mercy on his soul,” said the rough old Indian-fighter,
solemnly.
Kenton turned his head aside to brush away a tear. He had seen
many a death-scene, but none like this.
Again the dying man raised his head. A soft light now gleamed in his
blood-shot eyes.
“I see you,” and he extended his hand feebly toward the thicket.
Kenton and Boone looked in amazement, but they beheld nothing.
The sight was visible to the eyes of the stricken man, alone.
“See, she beckons me to come—no more blood, but peace—peace
and love eternal. I will come—see! she is there amid the cloud, I
come—wait.”
With a stifled gasp his head sunk back.
Boone could not repress a shudder, for he felt that he held a corpse
in his arms.
No more would the Wolf Demon carry terror to the hearts of the
Shawnee warriors.
With their hunting-knives the two scouts scooped a shallow grave
beneath the boughs of the hollow oak, and there, by the pale light
of the dying moon, they placed the mortal remains of Abe Lark, the
terrible Wolf Demon, the white husband of the Indian girl—Ke-ne-
ha-ha’s daughter—“The Red Arrow.”
The blood on Lark’s cap was easily accounted for by the woodmen
when they noticed a slight wound on the forehead of the body,
made by some bramble in the madman’s rapid flight through the
forest.
Boone and Kenton returned to Point Pleasant, and great was the
wonder of all when they learned who the Wolf Demon was.
The Indian expedition was abandoned. The death of the Shawnee
chieftain broke up the proposed confederacy.
Winthrop and Virginia were married in due time, much to the disgust
of Clement Murdock, who, shortly after, with Bob Tierson, emigrated
to Kentucky, and there met his death at the hands of the Regulators
for horse-stealing. Tierson, less guilty, escaped with a sound
thrashing.
Kate bore her cross with resignation, and none guessed the love that
was in her heart.
Our task is ended. The strange legend of the Wolf Demon is ended.
It is some six years since—with fishing-rod in hand—the writer
explored the pleasant tract of country bounded by the Scioto, the
Ohio, and the Muskingum; and he little dreamed then, when, in a
rude log-hut, an aged hunter told the strange old Indian legend, that
he should ever give to the world the story of the Red Arrow and the
Wolf Demon.

THE END.
Beadle’s Dime Library.
1 A Hard Crowd. By Philip S. Warne 10c.
2 The Dare-Devil. By Col. P. Ingraham 10c.
3 Kit Carson, Jr. By Buckskin Sam 10c.
4 The Kidnapper. By Philip S. Warne 10c.
5 The Fire Fiends. By A. P. Morris, Jr. 10c.
6 Wildcat Bob. By Edward L. Wheeler 10c.
7 Death-Notch, the Destroyer. Oll Coomes 10c.
8 The Headless Horseman. By Mayne Reid 10c.
9 Handy Andy. By Samuel Lover 10c.
10 Vidocq, the French Police Spy. Written by himself 10c.
11 Midshipman Easy. By Capt. Maryatt 10c.
12 The Death-Shot. By Capt. Mayne Reid 10c.
13 Pathaway; or, Nick Whiffles, the Old Trapper of the
Northwest. By Dr. J. H. Robinson 10c.
14 Thayendanegea, the Scourge. By Ned Buntline 10c.
15 The Tiger Slayer. By Gustave Aimard 10c.
16 The White Wizard. By Ned Buntline 10c.
17 Nightshade. By Dr. J. H. Robinson 10c.
18 The Sea Bandit. By Ned Buntline 10c.
19 Red Cedar. By Gustave Aimard 10c.
20 The Bandit at Bay. By Gustave Aimard 10c.
21 The Trapper’s Daughter. By Gustave Aimard 10c.
22 Whitelaw; or, Nattie of the Lake Shore. By Dr. J. H.
Robinson 10c.
23 The Red Warrior. By Ned Buntline 10c.
24 The Prairie Flower. By Gustave Aimard 10c.
25 The Gold Guide. By Francis Johnson 10c.
26 The Death Track. By Francis Johnson 10c.
27 The Spotter Detective. By Albert W. Aiken 10c.
28 Three-Fingered Jack, The Road-Agent of the Rockies. By
Joseph E. Badger, Jr. 10c.

You might also like