100% found this document useful (11 votes)
27 views

[Ebooks PDF] download Programming Algorithms in Lisp Writing Efficient Programs with Examples in ANSI Common Lisp 1st ed. Edition Vsevolod Domkin full chapters

Vsevolod

Uploaded by

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

[Ebooks PDF] download Programming Algorithms in Lisp Writing Efficient Programs with Examples in ANSI Common Lisp 1st ed. Edition Vsevolod Domkin full chapters

Vsevolod

Uploaded by

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

Download the full version of the ebook at ebookname.

com

Programming Algorithms in Lisp Writing Efficient


Programs with Examples in ANSI Common Lisp 1st ed.
Edition Vsevolod Domkin

https://ebookname.com/product/programming-algorithms-in-
lisp-writing-efficient-programs-with-examples-in-ansi-
common-lisp-1st-ed-edition-vsevolod-domkin/

OR CLICK BUTTON

DOWNLOAD EBOOK

Download more ebook instantly today at https://ebookname.com


Instant digital products (PDF, ePub, MOBI) available
Download now and explore formats that suit you...

Practical Common LISP 1st Corrected Edition Peter Seibel

https://ebookname.com/product/practical-common-lisp-1st-corrected-
edition-peter-seibel/

ebookname.com

Lisp in small pieces 1. paperback ed Edition Christian


Queinnec

https://ebookname.com/product/lisp-in-small-pieces-1-paperback-ed-
edition-christian-queinnec/

ebookname.com

Frontal Lisp Lateral Lisp Articulation and Oral Motor


Proceedures for Diagnosis and Treatment 1st Edition Pam
Marshalla
https://ebookname.com/product/frontal-lisp-lateral-lisp-articulation-
and-oral-motor-proceedures-for-diagnosis-and-treatment-1st-edition-
pam-marshalla/
ebookname.com

Entrepreneurship A Small Business Approach 1st Edition


Charles Bamford

https://ebookname.com/product/entrepreneurship-a-small-business-
approach-1st-edition-charles-bamford/

ebookname.com
Theatre Work Reimagining the Labor of Theatrical
Production 1st Edition Brídín Clements Cotton

https://ebookname.com/product/theatre-work-reimagining-the-labor-of-
theatrical-production-1st-edition-bridin-clements-cotton/

ebookname.com

Literature and the Long Modernity 1st Edition Mihaela


Irimia

https://ebookname.com/product/literature-and-the-long-modernity-1st-
edition-mihaela-irimia/

ebookname.com

The Flower Gardener s Bible A Complete Guide to Colorful


Blooms All Season Long 10th Anniversary Edition with a new
foreword by Suzy Bales Lewis Hill
https://ebookname.com/product/the-flower-gardener-s-bible-a-complete-
guide-to-colorful-blooms-all-season-long-10th-anniversary-edition-
with-a-new-foreword-by-suzy-bales-lewis-hill/
ebookname.com

B2B Selling For Dummies Hoover s Special Edition Tom


Hopkins

https://ebookname.com/product/b2b-selling-for-dummies-hoover-s-
special-edition-tom-hopkins/

ebookname.com

Close Relations an Introduction to the Sociology of


Families Mcdaniel

https://ebookname.com/product/close-relations-an-introduction-to-the-
sociology-of-families-mcdaniel/

ebookname.com
A Geography of the European Union 2nd Edition Francis Cole

https://ebookname.com/product/a-geography-of-the-european-union-2nd-
edition-francis-cole/

ebookname.com
Programming
Algorithms
in Lisp
Writing Efficient Programs with
Examples in ANSI Common Lisp

Vsevolod Domkin
Programming Algorithms
in Lisp
Writing Efficient Programs
with Examples in ANSI Common Lisp

Vsevolod Domkin
Programming Algorithms in Lisp: Writing Efficient Programs with Examples in ANSI
Common Lisp
Vsevolod Domkin
Kyiv, Ukraine

ISBN-13 (pbk): 978-1-4842-6427-0 ISBN-13 (electronic): 978-1-4842-6428-7


https://doi.org/10.1007/978-1-4842-6428-7

Copyright © 2021 Vsevolod Domkin


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
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 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 booktranslations@springernature.com; for reprint,
paperback, or audio rights, please e-mail bookpermissions@springernature.com.
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/9781484264270. For more
detailed information, please visit http://www.apress.com/source-­code.
Printed on acid-free paper
Table of Contents
About the Author����������������������������������������������������������������������������������������������������� ix

About the Technical Reviewer��������������������������������������������������������������������������������� xi


Acknowledgments������������������������������������������������������������������������������������������������� xiii

Chapter 1: Introduction�������������������������������������������������������������������������������������������� 1
Why Algorithms Matter����������������������������������������������������������������������������������������������������������������� 1
A Few Words About Lisp���������������������������������������������������������������������������������������������������������������� 3

Chapter 2: Algorithmic Complexity��������������������������������������������������������������������������� 7

Chapter 3: A Crash Course in Lisp�������������������������������������������������������������������������� 11


The Core of Lisp�������������������������������������������������������������������������������������������������������������������������� 11
A Code Example�������������������������������������������������������������������������������������������������������������������������� 13
The REPL������������������������������������������������������������������������������������������������������������������������������������� 14
Basic Expressions����������������������������������������������������������������������������������������������������������������������� 15
Sequential Execution������������������������������������������������������������������������������������������������������������� 16
Branching������������������������������������������������������������������������������������������������������������������������������ 17
Looping���������������������������������������������������������������������������������������������������������������������������������� 19
Procedures and Variables������������������������������������������������������������������������������������������������������ 21
Comments����������������������������������������������������������������������������������������������������������������������������� 26
Getting Started���������������������������������������������������������������������������������������������������������������������������� 27

Chapter 4: Data Structures������������������������������������������������������������������������������������� 29


Data Structures vs. Algorithms��������������������������������������������������������������������������������������������������� 29
The Data Structure Concept�������������������������������������������������������������������������������������������������������� 30
Contiguous and Linked Data Structures������������������������������������������������������������������������������������� 31
Tuples������������������������������������������������������������������������������������������������������������������������������������������ 32

iii
Table of Contents

Passing Data Structures in Function Calls���������������������������������������������������������������������������������� 34


Structs in Action: Union-Find������������������������������������������������������������������������������������������������������ 35
Takeaways���������������������������������������������������������������������������������������������������������������������������������� 39

Chapter 5: Arrays���������������������������������������������������������������������������������������������������� 41
Arrays as Sequences������������������������������������������������������������������������������������������������������������������ 43
Dynamic Vectors������������������������������������������������������������������������������������������������������������������������� 45
Why Are Arrays Indexed from 0��������������������������������������������������������������������������������������������������� 49
Multidimensional Arrays������������������������������������������������������������������������������������������������������������� 50
Binary Search������������������������������������������������������������������������������������������������������������������������������ 52
Binary Search in Action: A Fast Specialized In-Memory DB�������������������������������������������������� 58
Sorting���������������������������������������������������������������������������������������������������������������������������������������� 61
O(n^2) Sorting����������������������������������������������������������������������������������������������������������������������� 62
Quicksort������������������������������������������������������������������������������������������������������������������������������� 65
Production Sort���������������������������������������������������������������������������������������������������������������������� 68
Performance Benchmark������������������������������������������������������������������������������������������������������� 70
Takeaways���������������������������������������������������������������������������������������������������������������������������������� 72

Chapter 6: Linked Lists������������������������������������������������������������������������������������������� 75


Lists as Sequences��������������������������������������������������������������������������������������������������������������������� 77
Lists as Functional Data Structures�������������������������������������������������������������������������������������������� 80
Different Kinds of Lists���������������������������������������������������������������������������������������������������������������� 82
FIFO and LIFO������������������������������������������������������������������������������������������������������������������������������ 84
Queue������������������������������������������������������������������������������������������������������������������������������������ 84
Stack������������������������������������������������������������������������������������������������������������������������������������� 85
Deque������������������������������������������������������������������������������������������������������������������������������������ 89
Stacks in Action: SAX Parsing������������������������������������������������������������������������������������������������ 89
Lists as Sets�������������������������������������������������������������������������������������������������������������������������������� 92
Merge Sort���������������������������������������������������������������������������������������������������������������������������������� 94
Parallelization of Merge Sort������������������������������������������������������������������������������������������������� 97
Lists and Lisp������������������������������������������������������������������������������������������������������������������������������ 99
Takeaways���������������������������������������������������������������������������������������������������������������������������������� 99

iv
Table of Contents

Chapter 7: Key-­Values������������������������������������������������������������������������������������������ 101


Concrete Key-values����������������������������������������������������������������������������������������������������������������� 102
Simple Arrays���������������������������������������������������������������������������������������������������������������������� 103
Associative Lists������������������������������������������������������������������������������������������������������������������ 103
Hash-Tables������������������������������������������������������������������������������������������������������������������������� 105
Structs��������������������������������������������������������������������������������������������������������������������������������� 106
Trees������������������������������������������������������������������������������������������������������������������������������������ 107
Operations��������������������������������������������������������������������������������������������������������������������������������� 108
Memoization����������������������������������������������������������������������������������������������������������������������������� 110
Memoization in Action: Transposition Tables����������������������������������������������������������������������� 112
Cache Invalidation��������������������������������������������������������������������������������������������������������������������� 113
Second Chance and Clock Algorithms��������������������������������������������������������������������������������� 113
LFU�������������������������������������������������������������������������������������������������������������������������������������� 116
LRU�������������������������������������������������������������������������������������������������������������������������������������� 116
Low-Level Caching�������������������������������������������������������������������������������������������������������������������� 117
Takeaways�������������������������������������������������������������������������������������������������������������������������������� 119

Chapter 8: Hash-Tables����������������������������������������������������������������������������������������� 121


Implementation������������������������������������������������������������������������������������������������������������������������� 122
Dealing with Collisions�������������������������������������������������������������������������������������������������������� 122
Hash-Code��������������������������������������������������������������������������������������������������������������������������� 128
Advanced Hashing Techniques�������������������������������������������������������������������������������������������� 130
Hash-Functions������������������������������������������������������������������������������������������������������������������������� 131
Operations��������������������������������������������������������������������������������������������������������������������������������� 133
Initialization������������������������������������������������������������������������������������������������������������������������� 133
Access��������������������������������������������������������������������������������������������������������������������������������� 135
Iteration������������������������������������������������������������������������������������������������������������������������������� 136
Perfect Hashing������������������������������������������������������������������������������������������������������������������������� 138
Implementation������������������������������������������������������������������������������������������������������������������� 139
The CHM92 Algorithm���������������������������������������������������������������������������������������������������������� 141
Distributed Hash-Tables������������������������������������������������������������������������������������������������������������ 147
Hashing in Action: Content Addressing������������������������������������������������������������������������������������� 148
Takeaways�������������������������������������������������������������������������������������������������������������������������������� 151
v
Table of Contents

Chapter 9: Trees���������������������������������������������������������������������������������������������������� 153


Implementation Variants����������������������������������������������������������������������������������������������������������� 154
Tree Traversal���������������������������������������������������������������������������������������������������������������������������� 157
Binary Search Trees������������������������������������������������������������������������������������������������������������������ 163
Splay Trees�������������������������������������������������������������������������������������������������������������������������������� 165
Complexity Analysis������������������������������������������������������������������������������������������������������������� 172
Red-Black and AVL Trees���������������������������������������������������������������������������������������������������������� 175
B-Trees�������������������������������������������������������������������������������������������������������������������������������������� 177
Heaps���������������������������������������������������������������������������������������������������������������������������������������� 179
Tries������������������������������������������������������������������������������������������������������������������������������������������ 183
Trees in Action: Efficient Mapping��������������������������������������������������������������������������������������������� 188
Takeaways�������������������������������������������������������������������������������������������������������������������������������� 189

Chapter 10: Graphs����������������������������������������������������������������������������������������������� 191


Graph Representations������������������������������������������������������������������������������������������������������������� 192
Topological Sort������������������������������������������������������������������������������������������������������������������������ 193
MST������������������������������������������������������������������������������������������������������������������������������������������� 197
Prim’s Algorithm������������������������������������������������������������������������������������������������������������������ 197
Kruskal’s Algorithm������������������������������������������������������������������������������������������������������������� 204
Pathfinding�������������������������������������������������������������������������������������������������������������������������������� 204
Dijkstra’s Algorithm������������������������������������������������������������������������������������������������������������� 205
A* Algorithm������������������������������������������������������������������������������������������������������������������������ 206
Maximum Flow������������������������������������������������������������������������������������������������������������������������� 208
Graphs in Action: PageRank������������������������������������������������������������������������������������������������������ 211
Implementation������������������������������������������������������������������������������������������������������������������� 213
Takeaways�������������������������������������������������������������������������������������������������������������������������������� 215

Chapter 11: Strings����������������������������������������������������������������������������������������������� 217


Basic String-Related Optimizations������������������������������������������������������������������������������������������ 220
Strings in the Editor������������������������������������������������������������������������������������������������������������������ 221
Substring Search���������������������������������������������������������������������������������������������������������������������� 222
Knuth-Morris-Pratt (KMP)���������������������������������������������������������������������������������������������������� 223

vi
Table of Contents

Boyer-Moore (BM)���������������������������������������������������������������������������������������������������������������� 226


Rabin-Karp (RK)������������������������������������������������������������������������������������������������������������������� 227
Aho-Corasick (AC)���������������������������������������������������������������������������������������������������������������� 229
Regular Expressions����������������������������������������������������������������������������������������������������������������� 231
Implementation of Thompson’s Construction���������������������������������������������������������������������� 235
Grammars��������������������������������������������������������������������������������������������������������������������������������� 239
String Search in Action: Plagiarism Detection�������������������������������������������������������������������������� 249
Takeaways�������������������������������������������������������������������������������������������������������������������������������� 250

Chapter 12: Dynamic Programming��������������������������������������������������������������������� 251


Fibonacci Numbers������������������������������������������������������������������������������������������������������������������� 252
String Segmentation����������������������������������������������������������������������������������������������������������������� 254
Text Justification����������������������������������������������������������������������������������������������������������������������� 259
Pathfinding Revisited���������������������������������������������������������������������������������������������������������������� 264
LCS and Diff������������������������������������������������������������������������������������������������������������������������������ 266
DP in Action: Backprop�������������������������������������������������������������������������������������������������������������� 272
Takeaways�������������������������������������������������������������������������������������������������������������������������������� 275

Chapter 13: Approximation����������������������������������������������������������������������������������� 277


Combinatorial Optimization������������������������������������������������������������������������������������������������������� 277
Local Search����������������������������������������������������������������������������������������������������������������������������� 282
Evolutionary Algorithms������������������������������������������������������������������������������������������������������������ 288
Branch and Bound�������������������������������������������������������������������������������������������������������������������� 290
Gradient Descent���������������������������������������������������������������������������������������������������������������������� 293
Improving GD����������������������������������������������������������������������������������������������������������������������� 295
Sampling����������������������������������������������������������������������������������������������������������������������������������� 296
Matrix Factorization������������������������������������������������������������������������������������������������������������������ 299
Singular Value Decomposition��������������������������������������������������������������������������������������������� 299
Fourier Transform���������������������������������������������������������������������������������������������������������������������� 301
Fourier Transform in Action: JPEG��������������������������������������������������������������������������������������� 302
Takeaways�������������������������������������������������������������������������������������������������������������������������������� 303

vii
Table of Contents

Chapter 14: Compression������������������������������������������������������������������������������������� 305


Encoding����������������������������������������������������������������������������������������������������������������������������������� 305
Base64�������������������������������������������������������������������������������������������������������������������������������������� 306
Lossless Compression�������������������������������������������������������������������������������������������������������������� 311
Huffman Coding������������������������������������������������������������������������������������������������������������������������ 312
Huffman Coding in Action: Dictionary Optimization������������������������������������������������������������� 315
Arithmetic Coding��������������������������������������������������������������������������������������������������������������������� 328
DEFLATE������������������������������������������������������������������������������������������������������������������������������������ 334
Takeaways�������������������������������������������������������������������������������������������������������������������������������� 337

Chapter 15: Synchronization�������������������������������������������������������������������������������� 339


Synchronization Troubles���������������������������������������������������������������������������������������������������������� 341
Low-Level Synchronization������������������������������������������������������������������������������������������������������� 343
Mutual Exclusion Algorithms����������������������������������������������������������������������������������������������������� 345
High-Level Synchronization������������������������������������������������������������������������������������������������������ 347
Lock-Free Data Structures�������������������������������������������������������������������������������������������������� 348
Data Parallelism and Message Passing������������������������������������������������������������������������������� 350
STM������������������������������������������������������������������������������������������������������������������������������������� 350
Distributed Computations��������������������������������������������������������������������������������������������������������� 351
Distributed Algorithms��������������������������������������������������������������������������������������������������������� 352
Distributed Data Structures������������������������������������������������������������������������������������������������� 355
Distributed Algorithms in Action: Collaborative Editing������������������������������������������������������� 356
Persistent Data Structures�������������������������������������������������������������������������������������������������������� 359
Takeaways�������������������������������������������������������������������������������������������������������������������������������� 364

Afterword�������������������������������������������������������������������������������������������������������������� 367

Index��������������������������������������������������������������������������������������������������������������������� 369

viii
About the Author
Vsevolod Domkin from Kyiv, Ukraine, is a Lisp programmer and enthusiast, a natural
language processing researcher, an occasional writer/blogger, and a teacher.

ix
About the Technical Reviewer
Michał “phoe” Herda is a programmer with contributions
to multiple parts of the Common Lisp (CL) ecosystem: CL
implementations, existing and widely used CL utilities,
documentation, and some of the new library ideas that he
slowly pushes forward and works on.

xi
Acknowledgments
I’m very thankful to those who helped me in the work on Programming Algorithms in
Lisp by providing support, advice, corrections, and suggestions. First of all, many thanks
to my wife, Ksenya, who encouraged me to work on it despite the time that was, in part,
taken from my family duties. Michał “phoe” Herda contributed a very thorough and
detail-oriented review that helped correct a couple of significant misunderstandings on
my part and pushed me to add more code and explanations where they were lacking.
He has also championed the idea of a separate repository with all the book’s code
accompanied by a test suite and helped me in this undertaking.
I am very grateful to Dr. Robert Strandh who humbly volunteered his help as an
editor of the initial chapters of the book to make it sound more native (as my English
is far from perfect since I’m not a native speaker) and point out the mistakes that I
made. He and his wife, Kathleen, have contributed lots of improvements to more than
half of the chapters, and I tried to follow their advice in the subsequent ones. Thanks
to Rainer Joswig for commenting on the Lisp choices. I’m also grateful to my father,
Dr. Volodymyr Demkine, for reviewing and proofing the book. Thanks to @dzenicv on
reddit who posted links to almost all of the chapters there, which triggered some helpful
discussions. Thanks to @tom_mellior on Hacker News for pointing a serious deficiency
in the explanation of Union-Find. Thanks to all those people who shared the links to the
chapters and contributed their comments and attention.

xiii
CHAPTER 1

Introduction
This book started after teaching an intensive course on algorithms to working
programmers in Kyiv, in spring 2016. It took more than 3 years to complete, and,
meanwhile, I also did three iterations of the course. Its aim is to systematically explain
how to write efficient programs and, also, the approaches and tools for determining why
the program isn’t efficient enough. In the process, it will teach you some Lisp and show
in action the technics of algorithmic development. And, even if you won’t program in
Lisp afterward, you’ll still be able to utilize the same approaches and tools or be inclined
to ask why they aren’t available in your language of choice from its authors. :)

Why Algorithms Matter


In our industry, currently, there seems to prevail a certain misunderstanding of the
importance of algorithms for the working programmer. There’s often a disconnect between
the algorithmic questions posed at the job interviews and the everyday essence of the
same job. That’s why opinions are voiced that you, actually, don’t have to know CS to be
successful in the software developer’s job. That’s true. You don’t, but you’d better do if
you want to be in the notorious top 10% programmers. For several reasons. One is that,
actually, you can find room for algorithms almost at every corner of your work—provided
you are aware of their existence. To put it simply, the fact that you don’t know a more
efficient or elegant solution to a particular programming problem doesn’t make your code
less crappy. The current trend in software development is that, although the hardware
becomes more performant, the software becomes slower faster. There are two reasons for
that, in my humble opinion:

1. Most of the application programmers don’t know the inner


workings of the underlying platforms. And the number of platform
layers keeps increasing.

1
© Vsevolod Domkin 2021
V. Domkin, Programming Algorithms in Lisp, https://doi.org/10.1007/978-1-4842-6428-7_1
Chapter 1 Introduction

2. Most of the programmers also don’t know enough algorithms


and algorithmic development technics to squeeze the most from
their code. And often this means a loss of one or more orders of
magnitude of performance.

In the book, I’ll address, primarily, the second issue but will also try to touch on the
first whenever possible.
Besides, learning the art of solving difficult algorithmic problems trains the brain and
makes it more apt to solving various other problems, in the course of your day-to-day
work.
Finally, you will be speaking the same lingua franca as other advanced
programmers—the tongue that transcends the mundane differences of particular
programming languages. And you’ll gain a more detached view of those differences,
freeing your mind from the dictate of a particular set of choices exhibiting in any one of
them.
One of the reasons for this gap of understanding of the value of algorithms, probably,
originates from how they are usually presented in the computer science curriculum.
First, it is often done in a rather theoretical or “mathematical” way with rigorous proofs
and lack of connection to the real world. Second, the audience is usually freshmen or
sophomores who don’t have a lot of practical programming experience and thus can’t
appreciate and relate how this knowledge may be applied to their own programming
challenges (because they didn’t have those yet)—rather, most of them are still at the level
of struggling to learn well their first programming language and, in their understanding
of computing, are very much tied to its choices and idiosyncrasies.
In this book, the emphasis is made on the demonstration of the use of the described
data structures and algorithms in various areas of computer programming. Moreover,
I anticipate that the self-selected audience will comprise programmers with some
experience in the field. This makes a significant difference in the set of topics that are
relevant and how they can be conveyed. Another thing that helps a lot is when the
programmer has a good command of more than one programming language, especially
if the languages are from different paradigms: static and dynamic, object-oriented and
functional. These factors allow bridging the gap between “theoretical” algorithms and
practical coding, making the topic accessible, interesting, and inspiring.

2
Chapter 1 Introduction

This is one answer to a possible question: Why write another book on algorithms?
Indeed, there are several good textbooks and online courses on the topic, of which I’d
recommend the most Steven Skiena’s The Algorithm Design Manual. Yet, as I said, this
book is not at all academic in presentation of the material, which is a norm for other
textbooks. Except for simple arithmetic, it contains almost no “math” or proofs. And,
although proper attention is devoted to algorithmic complexity, it doesn’t deal with
theories of complexity or computation and similar scientific topics. Besides, all the
algorithms and data structures come with some example practical use cases. Last, but
not least, there’s no book on algorithms in Lisp, and, in my opinion, it’s a great topic
to introduce the language. The next chapter will provide a crash course to grasp the
basic ideas, and then we’ll discuss various Lisp programming approaches alongside the
algorithms they will be used to implement.
This is an introductory book, not a bible of algorithms. It will draw a comprehensive
picture and cover all topics necessary for further advancement of your algorithm
knowledge. However, it won’t go too deep into the advanced topics, such as persistent
or probabilistic data structures and advanced tree, graph, and optimization algorithms,
as well as algorithms for particular fields, such as machine learning, cryptography, or
computational geometry. All of those fields require (and usually have) separate books of
their own.

A Few Words About Lisp


For a long time, I’d been contemplating writing an introductory book on Lisp, but
something didn’t add up. I couldn’t see the coherent picture, in my mind. And then I
got a chance to teach algorithms with Lisp. From my point of view, it’s a perfect fit for
demonstrating data structures and algorithms (with a caveat that students should be
willing to learn it), while discussing the practical aspects of those algorithms allows to
explain the language naturally. At the same time, this topic requires almost no endeavor
into the adjacent areas of programming, such as architecture and program design,
integration with other systems, user interface, and use of advanced language features,
such as types or macros. And that is great because those topics are overkill for an
introductory text and they are also addressed nicely and in great detail elsewhere (see
Practical Common Lisp and ANSI Common Lisp).

3
Chapter 1 Introduction

Why Lisp is great for algorithmic programs? One reason is that the language was
created with such use case in mind. It has support for all the proper basic data structures,
such as arrays, hash-tables, linked lists, strings, and tuples. It also has a numeric tower,
which means no overflow errors and, so, a much saner math. Next, it’s created for the
interactive development style, so the experimentation cycle is very short, there’s no
compile-wait-run-revise red tape, and there are no unnecessary constraints, like the
need for additional annotations (a.k.a. types), prohibition of variable mutation, or other
stuff like that. You just write a function in the REPL (Read-Eval-Print Loop), run it, and
see the results. In my experience, Lisp programs look almost like pseudocode. Compared
to other languages, they may be slightly more verbose at times but are much more clear,
simple, and directly compatible with the algorithm’s logical representation.
But why not choose a popular programming language? The short answer is that it
wouldn’t have been optimal. There are four potential mainstream languages that could
be considered for this book: C++, Java, Python, and JavaScript (JS). (Surely, there’s
already enough material on algorithms that use them.) The first two are statically typed,
which is, in itself, a big obstacle to using them as teaching languages. Java is also too
verbose, while C++ too low level. These qualities don’t prevent them from being used
in the majority of production algorithm code, in the wild, and you’ll, probably, end
up dealing with such code sooner than later if not already. Besides, their standard
libraries provide great examples of practical algorithm implementation. But I believe
that gaining good conceptual understanding will allow to easily adapt to one of these
languages if necessary, while learning them in parallel with diving into algorithms
creates unnecessary complexity. Python and JS are, in many ways, the opposite choices:
they are dynamic and provide some level of an interactive experience (albeit inferior
compared to Lisp), but those languages are in many ways anti-algorithmic. Trying to be
simple and accessible, they hide too much from the programmer and don’t give enough
control of the concrete data. Teaching algorithms, using their standard libraries, seems
like cheating to me as their basic data structures often are not what they claim to be. Lisp
is in the middle: it is both highly interactive and gives enough control of the environment
while not being too verbose and demanding. And the price to pay—the unfamiliar
syntax—is really small, in my humble opinion.

4
Chapter 1 Introduction

Mostly, this book will be dedicated to showing Lisp code and explaining it. Yet, all
such code snippets will fall into two quite different categories:

• One kind will represent complete code blocks (with occasional


small omissions left as exercises for you) that could be run in the
Lisp environment, accompanied with the examples of its invocation.
These code blocks are accessible in a dedicated GitHub repository.

• The other kind is represented by sketches used to explain how the


presented algorithms will be built into larger systems (usually, you’ll
see these sketches in the “in action” section of each chapter). Such
sketches will not be runnable as they may require a lot of supporting
code and/or infrastructure and should be treated only as an outline.

5
CHAPTER 2

Algorithmic Complexity
Complexity is a point that will be mentioned literally on every page of this book; the
discussion of any algorithm or data structure can’t avoid this topic. After correctness,
it is the second most important quality of every algorithm. Moreover, often correctness
alone doesn’t matter if complexity is neglected, while the opposite is possible: to
compromise correctness somewhat in order to get significantly better complexity. By
and large, algorithm theory differs from other subjects of CS in that it concerns not about
presenting a working (correct) way to solve some problem but about finding an efficient
way to do it, where efficiency is understood as the minimal (or admissible) number of
operations performed and occupied memory space.
In principle, the complexity of an algorithm is the dependence of the number of
operations that will be performed on the size of the input. It is crucial to the computer
system’s scalability: it may be easy to solve the programming problem for a particular set
of inputs, but how will the solution behave if the input is doubled or increased tenfold or
millionfold? This is not a theoretical question, and an analysis of any general-purpose
algorithm should have a clear answer to it.
Complexity is a substantial research topic: a whole separate branch of CS—
complexity theory—exists to study it. Yet, throughout the book, we’ll try to utilize the
end results of such research without delving deep into rigorous proofs or complex
math, especially since, in most of the cases, measuring complexity is a matter of simple
counting. Let’s look at the following illustrative example:

(defun mat-max (mat)


  (let (max)
    (dotimes (i (array-dimension mat 0))
      (dotimes (j (array-dimension mat 1))
        (when (or (null max)
                  (> (aref mat i j) max))
          (setf max (aref mat i j)))))
    max))
7
© Vsevolod Domkin 2021
V. Domkin, Programming Algorithms in Lisp, https://doi.org/10.1007/978-1-4842-6428-7_2
Chapter 2 Algorithmic Complexity

This function finds the maximum element of a two-dimensional array (matrix):

CL-USER> (mat-max #2A((1 2 3) (4 5 6)))


6

What’s its complexity? To answer, we can just count the number of operations
performed: at each iteration of the inner loop, there are two comparisons involving
one array access, and, sometimes, if the planets align, we perform another access for
assignment. The inner loop is executed (array-dimension mat 1) times (let’s call it m
where m=3) and the outer one (array-dimension mat 0) (n=2, in the example). If we
sum this all up, we’ll get n * m * 4 as an upper limit, for the worst case when each
sequent array element is larger than the previous. As a rule of thumb, each loop adds
multiplication to the formula, and each sequential block adds a plus sign.
In this calculation, there are two variables (array dimensions n and m) and one
constant (the number of operations performed for each array element). There exists
a special notation—Big-O—used to simplify the representation of end results of such
complexity arithmetic. In it, all constants are reduced to 1, and thus m * 1 becomes just
m, and also since we don’t care about individual array dimension differences, we can
just put n * n instead of n * m. With such simplification, we can write down the final
complexity result for this function: O(nˆ2). In other words, our algorithm has quadratic
complexity (which happens to be a variant of a broader class called “polynomial
complexity”) in array dimensions. It means that by increasing the dimensions of our
matrix ten times, we’ll increase the number of operations of the algorithm 100 times.
In this case, however, it may be more natural to be concerned with the dependence of
the number of operations on the number of elements of the matrix, not its dimensions.
We can observe that nˆ2 is the actual number of elements, so it can also be written as
just n—if by n we mean the number of elements and then the complexity is linear in the
number of elements (O(n)). As you see, it is crucial to understand what n we are talking
about!
There are just a few more things to know about Big-O complexity before we can start
using it to analyze our algorithms:

1. There are six major complexity classes of algorithms:


• Constant-time (O(1))

• Sublinear (usually, logarithmic—O(log n))

• Linear (O(n)) and superlinear (O(n * log n))

8
Chapter 2 Algorithmic Complexity

• Higher-order polynomial (O(nˆc), where c is some constant


greater than 1)

• Exponential (O(cˆn), where c is usually 2 but, at least, greater


than 1)

• Just plain lunatic complex (O(n!) and so forth)—I call them


O(mg), jokingly

Each class is a step-function change in performance, especially, at


scale. We’ll talk about each one of them as we’ll be discussing the
particular examples of algorithms falling into it.

2. Worst-case vs. average-case behavior. In this example, we saw


that there may be two counts of operations: for the average case,
we can assume that approximately half of the iterations will
require assignment (which results in 3,5 operations in each inner
loop), and, for the worst case, the number will be exactly 4. As
Big-O reduces all numbers to 1, for this example, the difference
is irrelevant, but there may be others, for which it is much more
drastic and can’t be discarded. Usually, for such algorithms, both
complexities should be mentioned (alongside ways to avoid
worst-case scenarios): a good example is quicksort algorithm
described in Chapter 5.

3. We have also seen the so-called “constant factors hidden by the


Big-O notation.” That is, from the point of view of algorithmic
complexity, it doesn’t matter if we need to perform 3 operations in
the inner loop or 30. Yet, it is quite important in practice, and we’ll
also discuss it when examining binary search. Even more, some
algorithms with better theoretical complexity may be worse in
many practical applications due to these hidden factors (e.g., until
the dataset reaches a certain size).

4. Finally, besides execution time complexity, there’s also space


complexity, which instead of the number of operations measures
the amount of storage space used proportional to the size of the
input. In general, similar approaches are applied to its estimation.

9
CHAPTER 3

A Crash Course in Lisp


The introductory post for this book, unexpectedly, received quite a lot of attention,
which is nice since it prompted some questions, and one of them I planned to address in
this chapter.
I expect that there will be two main audiences for this book:

• People who’d like to advance in algorithms and writing efficient


programs—the major group

• Lispers, either accomplished or aspiring, who also happen to be


interested in algorithms

This chapter is intended primarily for the first group. After reading it, the rest of the
Lisp code from the book should become understandable to you. Besides, you’ll know the
basics to run Lisp and experiment with it if you will so desire.
As for the lispers, you might be interested to glance over this part just to understand
my approach to utilizing the language throughout the book.

The Core of Lisp


To effortlessly understand Lisp, you’ll have to forget, for a moment, any concepts of how
programming languages should work that you might have acquired from your prior
experience in coding. Lisp is simpler; and when people bring their Java, C, or Python
approaches to programming with it, first of all, the results are suboptimal in terms
of code quality (simplicity, clarity, and beauty), and, what’s more important, there’s
much less satisfaction from the process, not to mention very few insights and little new
knowledge gained.

11
© Vsevolod Domkin 2021
V. Domkin, Programming Algorithms in Lisp, https://doi.org/10.1007/978-1-4842-6428-7_3
Chapter 3 A Crash Course in Lisp

It is much easier to explain Lisp if we begin from a blank slate. In essence, all there is
to it is just an evaluation rule: Lisp programs consist of forms that are evaluated by the
compiler. There are 3 + 2 ways how that can happen:

• Self-evaluation: All literal constants (like 1, "hello", etc.) are


evaluated to themselves. These literal objects can be either built-in
primitive types (1) or data structures ("hello").

• Symbol evaluation: Separate symbols are evaluated as names of


variables, functions, types, or classes depending on the context. The
default is variable evaluation, that is, if we encounter a symbol foo,
the compiler will substitute in its place the current value associated
with this variable (more on this a little bit later).

• Expression evaluation: Compound expressions are formed by


grouping symbols and literal objects with parentheses. The form
(oper 1 foo) is considered a “functional” expression: the operator
name is situated in the first position (head) and its arguments, if any,
in the subsequent positions (rest).

There are three ways to evaluate a Lisp compound expression:

• There are 25 special operators that are defined in lower-level code


and may be considered something like axioms of the language:
they are predefined, always present, and immutable. Those are the
building blocks, on top of which all else is constructed, and they
include the sequential block operator, the conditional expression if,
and the unconditional jump go, to name a few. If oper is the name of
a special operator, the low-level code for this operator that deals with
the arguments in its own unique way is executed.

• There’s also ordinary function evaluation: if oper is a function name,


first, all the arguments are evaluated with the same evaluation rule,
and then the function is called with the obtained values.

12
Chapter 3 A Crash Course in Lisp

• Finally, there’s macro evaluation. Macros provide a way to change the


evaluation rule for a particular form. If oper names a macro, its code
is substituted instead of our expression and then evaluated. Macros
are a major topic in Lisp, and they are used to build a large part of the
language as well as provide an accessible way, for the users, to extend
it. However, they are orthogonal to the subject of this book and won’t
be discussed in further detail here. You can delve deeper into macros
in such books as On Lisp or Let Over Lambda.
It’s important to note that, in Lisp, there’s no distinction between statements and
expressions, no special keywords, no operator precedence rules, and other similar
arbitrary stuff you can stumble upon in other languages. Everything is uniform;
everything is an expression in a sense that it will be evaluated and return some value.

A Code Example
To sum up, let’s consider an example of the evaluation of a Lisp form. The following one
implements the famous binary search algorithm (that we’ll discuss in more detail in one
of the following chapters):

(when (> (length vec) 0)


  (let ((beg 0)
        (end (length vec)))
    (do ()
        ((= beg end))
      (let ((mid (floor (+ beg end) 2)))
        (if (> (aref vec mid) val)
            (setf end mid)
            (setf beg (1+ mid)))))
    (values beg
            (aref vec beg)
            (= (aref vec beg) val))))

13
Chapter 3 A Crash Course in Lisp

It is a compound form. In it, the so-called top-level form is when, which is a macro for
a one-clause conditional expression: an if with only the true branch. First, it evaluates
the expression (> (length vec) 0), which is an ordinary function for a logical operator
> applied to two args: the result of obtaining the length of the contents of the variable
vec and a constant 0. If the evaluation returns true, that is, the length of vec is greater
than 0, the rest of the form is evaluated in the same manner. The result of the evaluation,
if nothing exceptional happens, is either false (which is called nil, in Lisp) or three
values returned from the last form (values ...). In the following, we’ll talk about other
operators shown here.
But first I need to say a few words about RUTILS. It is a third-party library developed
by me that provides a number of extensions to the standard Lisp syntax and its basic
operators. The reason for its existence is that the Lisp standard is not going to change
ever, and, as everything in this world, it has its flaws. Besides, our understanding of what
constitutes elegant and efficient code evolves over time. The great advantage of the Lisp
standard, however, which counteracts the issue of its immutability, is that its authors had
put into it multiple ways to modify and evolve the language at almost all levels starting
from even the basic syntax. And this addresses our ultimate need, after all: we’re not
so interested in changing the standard as in changing the language. So RUTILS is one of
the ways of evolving Lisp; and its purpose is to make programming in it more accessible
without compromising the core principles of the language. So I will utilize a number
of RUTILS features throughout this book, explaining them as needed. Surely, using a
particular third-party extension is a question of preference and taste, and it might not
be approved by some of the Lisp old-times, but no worries: in your code, you’ll be able
to easily swap them for your favorite alternatives. Yet, completely rejecting this option is
puristic and impractical.

The REPL
Lisp programs are supposed to be run not only in a one-off fashion of simple scripts but also
as live systems that operate over long periods of time experiencing change not only of their
data but also code. This general way of interaction with a program is called Read-Eval-Print
Loop (REPL), which literally means that the Lisp compiler reads a form, evaluates it with the
aforementioned rule, prints the results back to the user, and loops over.

14
Chapter 3 A Crash Course in Lisp

REPL is the default way to interact with a Lisp program, and it is very similar to
the Unix shell. When you run your Lisp (e.g., by entering sbcl at the shell), you’ll drop
into the REPL. We’ll precede all REPL-based code interactions in the book with a REPL
prompt (CL-USER> or similar). Here’s an example one:

CL-USER> (print "Hello world")


"Hello world"
"Hello world"

A curious reader may be asking why "Hello world" is printed twice. It’s a proof
that everything is an expression in Lisp. :) The print “statement,” unlike in most other
languages, not only prints its argument to the console (or other output streams) but also
returns it as is. This comes very handy when debugging, as you can wrap almost any
form in a print not changing the flow of the program.
Obviously, if the interaction is not necessary, just the read-eval part may remain. But,
what’s more important, Lisp provides a way to customize every stage of the process:

• At the read stage, special syntax (“syntax sugar”) may be introduced


via a mechanism called reader macros.

• Ordinary macros are a way to customize the eval stage.

• The print stage is conceptually the simplest one, and there’s also
a standard way to customize object printing via the Common Lisp
Object System’s (CLOS) print-object function.

• The loop stage can be replaced by any desired program logic.

Basic Expressions
The structural programming paradigm states that all programs can be expressed in terms
of three basic constructs: sequential execution, branching, and looping. Let’s see how
these operators are expressed in Lisp.

15
Random documents with unrelated
content Scribd suggests to you:
the English shot made larger holes than the Spanish, and a few more men
would have turned the scale and given the victory to the Dainty.

This breathing time the English employed in repairing sails and


tackling, stopping leaks, mending pumps, and splicing yards; for they had
many shot under water, and the pumps were battered to pieces.

When the action was renewed the vice-admiral came upon their quarter,
and a shot from one of the Dainty's stern-pieces carried away his mainmast
close to the deck. Hawkins lay below, and knew nothing of what had
occurred; then was the time to press the Spaniard home, but the Dainty was
steered away, and the Spaniards had time to repair their damage.

They soon overtook the Dainty, and the fight went on through the
second night, and they ceased firing again before the dawn; but there had
been no interval for rest or refreshment, except to snatch a little bread and
wine as they could. Indeed, some of the English crew had drunk heavily
before the fight began; some ignorant seamen even mixed powder with their
wine, thinking it would give them strength and courage. The result of their
drinking was, of course, order, and foolish hardihood without reason, or
vainglorious exposure to danger. And though Hawkins had prepared light
armour for all, not a man would use it; yet it would have saved many from
such wounds as splintered wood creates if they would have imitated their
foe and worn armour.

By the afternoon of the third day the enemy had the weather-gage of
them, and their guns were telling with terrible effect.

The Dainty had now fourteen gaping wounds under water, eight foot of
water in her hold, her sails torn to tatters, her masts bowing and bent, and
her pumps useless—hardly a man was now unwounded.

Again the master with others approached their commander:

"Sir, the Spaniards still offer good war, life and liberty and an
embarkation to England. If we wait any longer, sir, the ship will sink; unless
a miracle be wrought in our behalf by God's almighty power, we may
expect no deliverance or life."
Hawkins was too ill to resist further; he murmured sadly:

"Haul down the ensign, then, and hoist a flag of truce."

So they bade the rest cease firing, and a Spanish prisoner was sent from
the hold to tell Beltran de Castro that if he would give his word of honour
the ship should be surrendered.

Seeing the flag of truce, the Spaniard shouted:

"Hoist out your boat, Englishman."

"We cannot do so; it be all shot to pieces."

"So is ours. Amain your sails, then; strike sail, can't you?"

"No, we can't; there be not enough men left to handle them."

Meanwhile the vice-admiral, not seeing the flag of truce, had come
upon the Dainty's quarter, and firing two of his chase-pieces, wounded the
captain sorely in the thigh and maimed one of the master's mates.

Then the Spanish admiral came alongside, and the prisoner jumped into
the warship, and was received with all courtesy.

Don Beltran affirmed that he received the commander and his people à
buena guerra, to the laws of fair war and quarter. He swore by his habit of
Alcantara, and the green cross of the order which he wore upon his breast,
that he would give them their lives with good treatment, and send them as
speedily as he could to their own country.

"The Spanish admiral wants a pledge? Here is my glove; take it to him."

Don Beltran also sent one of his captains to help to bring the English
commander aboard the "admiral," which he did with great humanity and
courtesy.

"The General received me," says Hawkins, "with courtesy and


compassion, even with tears in his eyes and words of kind consolation, and
commanded me to be accommodated in his own cabin, where he sought to
cure and comfort me the best he could: the like he did with all our hurt
men."

There were only forty Englishmen left, all wounded; but all recovered,
in spite of the fact that no instruments, doctors, or salves were to be had. We
remember that in the other case where an English ship had to surrender to
the Spaniards, the Revenge disdained to swim in dishonour, and sank
sullenly in a terrible storm.

The Dainty lived to fight for Spain under the name of La Visitation,
being so named because she was captured on the day of that festival.

As soon as Hawkins was removed the Spaniards began to ransack their


prize; but the water increased so fast in the hold that she nearly sank, and it
needed a strong body of workers to save her.

She was finally navigated to the port of Panama, and anchored there
some two leagues from the town, about three weeks after the fight.

When the good folk on shore saw the prize and heard the glad news,
they lit bonfires on the hills and candles in every window; the churches and
halls were illuminated, as on a holy day. As the city faced the sea, it
appeared to those in the ships as though the whole place was in flames.

Don Beltran reassured Hawkins that his officers and men should be well
treated, and gave him his word that if the King left him to his disposal, his
ransom should be only a couple of greyhounds for himself and a couple for
his brother. It sounded almost too good to be true.

Then the Englishman had the mortification of seeing his dear Dainty
being rebaptized with all solemnity in the harbour, where she was shored
up. Perhaps a sardonic smile curled his lip when, in the very midst of the
ceremony, the props on one side gave way with a loud crash, and the
reluctant ship heeled over, "entreating many of them that were in her very
badly."
Here ends Sir Richard's account of his unfortunate voyage in his
"Observations"; he had intended to write a second part, but deferred it too
long.

Don Beltran was not allowed by his King to observe the terms he had
offered; the crew were sent to serve in the galleys at Cartagena, Hawkins
and twenty others Don Beltran took with him to Lima.

Our hero had shown courage and generosity and kindness to natives and
prisoners, but as a complete seaman his own words show him to have been
deficient. He trusted his subordinates too much, and he kept rather loose
discipline; but he was a man of the highest honour, and won the respect of
the best Spaniards.

At Lima the Inquisition claimed the prisoners, but the Viceroy refused
to give them up until he had heard from King Philip.

In 1597 Hawkins was sent to Spain and imprisoned at Seville; in


September 1598 he escaped, but was retaken and thrust into a dungeon.

In 1599 he was taken to Madrid, although Don Beltran had indignantly


protested against the violation of his solemn promise.

In 1602 he was released and sent home, as by this time Count Miranda,
President of the Council, had come to the conclusion that formal pledges
given by the King's officers must be kept, or else no other English would
surrender.

In July 1603 Hawkins was knighted, became M.P. for Plymouth and
Vice-Admiral of Devon, and had to scour the sea for pirates.

In 1620 he sailed under Sir Robert Mansell to put down Algerine


corsairs in the Mediterranean, and returned home, after a failure, sick and
weak in body. In 1622 he was carried off by a fit while attending the Privy
Council on business bearing on his late command.

By his wife Judith he left two sons and four daughters.


His book, "Observations in his Voyage into the South Sea, A.D. 1593,"
was not written until nearly thirty years after the events, and consequently
bears traces of inaccuracy in details and dates; but it surpasses all other
books of travel of those times in describing the details of nautical life, in
scientific interest concerning the fauna and flora of the countries he visited,
and in transparent candour and freedom from prejudice. He was no boastful
discoverer, but a God-fearing, conscientious servant of the Queen, who, like
so many others, tried to do his duty, and sometimes failed to reach the
highest success.

But for all that, he was not the least among England's heroes; he was a
worthy son of Sir John, and a man whom Devon may claim as one of her
noblest and most generous sons.

Printed by BALLANTYNE, HANSON & Co.


Edinburgh & London
*** END OF THE PROJECT GUTENBERG EBOOK STORIES OF
ELIZABETHAN HEROES ***

Updated editions will replace the previous one—the old editions will
be renamed.

Creating the works from print editions not protected by U.S.


copyright law means that no one owns a United States copyright in
these works, so the Foundation (and you!) can copy and distribute it
in the United States without permission and without paying
copyright royalties. Special rules, set forth in the General Terms of
Use part of this license, apply to copying and distributing Project
Gutenberg™ electronic works to protect the PROJECT GUTENBERG™
concept and trademark. Project Gutenberg is a registered trademark,
and may not be used if you charge for an eBook, except by following
the terms of the trademark license, including paying royalties for use
of the Project Gutenberg trademark. If you do not charge anything
for copies of this eBook, complying with the trademark license is
very easy. You may use this eBook for nearly any purpose such as
creation of derivative works, reports, performances and research.
Project Gutenberg eBooks may be modified and printed and given
away—you may do practically ANYTHING in the United States with
eBooks not protected by U.S. copyright law. Redistribution is subject
to the trademark license, especially commercial redistribution.

START: FULL LICENSE


THE FULL PROJECT GUTENBERG LICENSE
PLEASE READ THIS BEFORE YOU DISTRIBUTE OR USE THIS WORK

To protect the Project Gutenberg™ mission of promoting the free


distribution of electronic works, by using or distributing this work (or
any other work associated in any way with the phrase “Project
Gutenberg”), you agree to comply with all the terms of the Full
Project Gutenberg™ License available with this file or online at
www.gutenberg.org/license.

Section 1. General Terms of Use and


Redistributing Project Gutenberg™
electronic works
1.A. By reading or using any part of this Project Gutenberg™
electronic work, you indicate that you have read, understand, agree
to and accept all the terms of this license and intellectual property
(trademark/copyright) agreement. If you do not agree to abide by all
the terms of this agreement, you must cease using and return or
destroy all copies of Project Gutenberg™ electronic works in your
possession. If you paid a fee for obtaining a copy of or access to a
Project Gutenberg™ electronic work and you do not agree to be
bound by the terms of this agreement, you may obtain a refund
from the person or entity to whom you paid the fee as set forth in
paragraph 1.E.8.

1.B. “Project Gutenberg” is a registered trademark. It may only be


used on or associated in any way with an electronic work by people
who agree to be bound by the terms of this agreement. There are a
few things that you can do with most Project Gutenberg™ electronic
works even without complying with the full terms of this agreement.
See paragraph 1.C below. There are a lot of things you can do with
Project Gutenberg™ electronic works if you follow the terms of this
agreement and help preserve free future access to Project
Gutenberg™ electronic works. See paragraph 1.E below.
1.C. The Project Gutenberg Literary Archive Foundation (“the
Foundation” or PGLAF), owns a compilation copyright in the
collection of Project Gutenberg™ electronic works. Nearly all the
individual works in the collection are in the public domain in the
United States. If an individual work is unprotected by copyright law
in the United States and you are located in the United States, we do
not claim a right to prevent you from copying, distributing,
performing, displaying or creating derivative works based on the
work as long as all references to Project Gutenberg are removed. Of
course, we hope that you will support the Project Gutenberg™
mission of promoting free access to electronic works by freely
sharing Project Gutenberg™ works in compliance with the terms of
this agreement for keeping the Project Gutenberg™ name associated
with the work. You can easily comply with the terms of this
agreement by keeping this work in the same format with its attached
full Project Gutenberg™ License when you share it without charge
with others.

1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the
terms of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.

1.E. Unless you have removed all references to Project Gutenberg:

1.E.1. The following sentence, with active links to, or other


immediate access to, the full Project Gutenberg™ License must
appear prominently whenever any copy of a Project Gutenberg™
work (any work on which the phrase “Project Gutenberg” appears,
or with which the phrase “Project Gutenberg” is associated) is
accessed, displayed, performed, viewed, copied or distributed:
This eBook is for the use of anyone anywhere in the United
States and most other parts of the world at no cost and with
almost no restrictions whatsoever. You may copy it, give it away
or re-use it under the terms of the Project Gutenberg License
included with this eBook or online at www.gutenberg.org. If you
are not located in the United States, you will have to check the
laws of the country where you are located before using this
eBook.

1.E.2. If an individual Project Gutenberg™ electronic work is derived


from texts not protected by U.S. copyright law (does not contain a
notice indicating that it is posted with permission of the copyright
holder), the work can be copied and distributed to anyone in the
United States without paying any fees or charges. If you are
redistributing or providing access to a work with the phrase “Project
Gutenberg” associated with or appearing on the work, you must
comply either with the requirements of paragraphs 1.E.1 through
1.E.7 or obtain permission for the use of the work and the Project
Gutenberg™ trademark as set forth in paragraphs 1.E.8 or 1.E.9.

1.E.3. If an individual Project Gutenberg™ electronic work is posted


with the permission of the copyright holder, your use and distribution
must comply with both paragraphs 1.E.1 through 1.E.7 and any
additional terms imposed by the copyright holder. Additional terms
will be linked to the Project Gutenberg™ License for all works posted
with the permission of the copyright holder found at the beginning
of this work.

1.E.4. Do not unlink or detach or remove the full Project


Gutenberg™ License terms from this work, or any files containing a
part of this work or any other work associated with Project
Gutenberg™.

1.E.5. Do not copy, display, perform, distribute or redistribute this


electronic work, or any part of this electronic work, without
prominently displaying the sentence set forth in paragraph 1.E.1
with active links or immediate access to the full terms of the Project
Gutenberg™ License.

1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if you
provide access to or distribute copies of a Project Gutenberg™ work
in a format other than “Plain Vanilla ASCII” or other format used in
the official version posted on the official Project Gutenberg™ website
(www.gutenberg.org), you must, at no additional cost, fee or
expense to the user, provide a copy, a means of exporting a copy, or
a means of obtaining a copy upon request, of the work in its original
“Plain Vanilla ASCII” or other form. Any alternate format must
include the full Project Gutenberg™ License as specified in
paragraph 1.E.1.

1.E.7. Do not charge a fee for access to, viewing, displaying,


performing, copying or distributing any Project Gutenberg™ works
unless you comply with paragraph 1.E.8 or 1.E.9.

1.E.8. You may charge a reasonable fee for copies of or providing


access to or distributing Project Gutenberg™ electronic works
provided that:

• You pay a royalty fee of 20% of the gross profits you derive
from the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”

• You provide a full refund of any money paid by a user who


notifies you in writing (or by e-mail) within 30 days of receipt
that s/he does not agree to the terms of the full Project
Gutenberg™ License. You must require such a user to return or
destroy all copies of the works possessed in a physical medium
and discontinue all use of and all access to other copies of
Project Gutenberg™ works.

• You provide, in accordance with paragraph 1.F.3, a full refund of


any money paid for a work or a replacement copy, if a defect in
the electronic work is discovered and reported to you within 90
days of receipt of the work.

• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.

1.E.9. If you wish to charge a fee or distribute a Project Gutenberg™


electronic work or group of works on different terms than are set
forth in this agreement, you must obtain permission in writing from
the Project Gutenberg Literary Archive Foundation, the manager of
the Project Gutenberg™ trademark. Contact the Foundation as set
forth in Section 3 below.

1.F.

1.F.1. Project Gutenberg volunteers and employees expend


considerable effort to identify, do copyright research on, transcribe
and proofread works not protected by U.S. copyright law in creating
the Project Gutenberg™ collection. Despite these efforts, Project
Gutenberg™ electronic works, and the medium on which they may
be stored, may contain “Defects,” such as, but not limited to,
incomplete, inaccurate or corrupt data, transcription errors, a
copyright or other intellectual property infringement, a defective or
damaged disk or other medium, a computer virus, or computer
codes that damage or cannot be read by your equipment.

1.F.2. LIMITED WARRANTY, DISCLAIMER OF DAMAGES - Except for


the “Right of Replacement or Refund” described in paragraph 1.F.3,
the Project Gutenberg Literary Archive Foundation, the owner of the
Project Gutenberg™ trademark, and any other party distributing a
Project Gutenberg™ electronic work under this agreement, disclaim
all liability to you for damages, costs and expenses, including legal
fees. YOU AGREE THAT YOU HAVE NO REMEDIES FOR
NEGLIGENCE, STRICT LIABILITY, BREACH OF WARRANTY OR
BREACH OF CONTRACT EXCEPT THOSE PROVIDED IN PARAGRAPH
1.F.3. YOU AGREE THAT THE FOUNDATION, THE TRADEMARK
OWNER, AND ANY DISTRIBUTOR UNDER THIS AGREEMENT WILL
NOT BE LIABLE TO YOU FOR ACTUAL, DIRECT, INDIRECT,
CONSEQUENTIAL, PUNITIVE OR INCIDENTAL DAMAGES EVEN IF
YOU GIVE NOTICE OF THE POSSIBILITY OF SUCH DAMAGE.

1.F.3. LIMITED RIGHT OF REPLACEMENT OR REFUND - If you


discover a defect in this electronic work within 90 days of receiving
it, you can receive a refund of the money (if any) you paid for it by
sending a written explanation to the person you received the work
from. If you received the work on a physical medium, you must
return the medium with your written explanation. The person or
entity that provided you with the defective work may elect to provide
a replacement copy in lieu of a refund. If you received the work
electronically, the person or entity providing it to you may choose to
give you a second opportunity to receive the work electronically in
lieu of a refund. If the second copy is also defective, you may
demand a refund in writing without further opportunities to fix the
problem.

1.F.4. Except for the limited right of replacement or refund set forth
in paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO
OTHER WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.

1.F.5. Some states do not allow disclaimers of certain implied


warranties or the exclusion or limitation of certain types of damages.
If any disclaimer or limitation set forth in this agreement violates the
law of the state applicable to this agreement, the agreement shall be
interpreted to make the maximum disclaimer or limitation permitted
by the applicable state law. The invalidity or unenforceability of any
provision of this agreement shall not void the remaining provisions.

1.F.6. INDEMNITY - You agree to indemnify and hold the Foundation,


the trademark owner, any agent or employee of the Foundation,
anyone providing copies of Project Gutenberg™ electronic works in
accordance with this agreement, and any volunteers associated with
the production, promotion and distribution of Project Gutenberg™
electronic works, harmless from all liability, costs and expenses,
including legal fees, that arise directly or indirectly from any of the
following which you do or cause to occur: (a) distribution of this or
any Project Gutenberg™ work, (b) alteration, modification, or
additions or deletions to any Project Gutenberg™ work, and (c) any
Defect you cause.

Section 2. Information about the Mission


of Project Gutenberg™
Project Gutenberg™ is synonymous with the free distribution of
electronic works in formats readable by the widest variety of
computers including obsolete, old, middle-aged and new computers.
It exists because of the efforts of hundreds of volunteers and
donations from people in all walks of life.

Volunteers and financial support to provide volunteers with the


assistance they need are critical to reaching Project Gutenberg™’s
goals and ensuring that the Project Gutenberg™ collection will
remain freely available for generations to come. In 2001, the Project
Gutenberg Literary Archive Foundation was created to provide a
secure and permanent future for Project Gutenberg™ and future
generations. To learn more about the Project Gutenberg Literary
Archive Foundation and how your efforts and donations can help,
see Sections 3 and 4 and the Foundation information page at
www.gutenberg.org.

Section 3. Information about the Project


Gutenberg Literary Archive Foundation
The Project Gutenberg Literary Archive Foundation is a non-profit
501(c)(3) educational corporation organized under the laws of the
state of Mississippi and granted tax exempt status by the Internal
Revenue Service. The Foundation’s EIN or federal tax identification
number is 64-6221541. Contributions to the Project Gutenberg
Literary Archive Foundation are tax deductible to the full extent
permitted by U.S. federal laws and your state’s laws.

The Foundation’s business office is located at 809 North 1500 West,


Salt Lake City, UT 84116, (801) 596-1887. Email contact links and up
to date contact information can be found at the Foundation’s website
and official page at www.gutenberg.org/contact

Section 4. Information about Donations to


the Project Gutenberg Literary Archive
Foundation
Project Gutenberg™ depends upon and cannot survive without
widespread public support and donations to carry out its mission of
increasing the number of public domain and licensed works that can
be freely distributed in machine-readable form accessible by the
widest array of equipment including outdated equipment. Many
small donations ($1 to $5,000) are particularly important to
maintaining tax exempt status with the IRS.

The Foundation is committed to complying with the laws regulating


charities and charitable donations in all 50 states of the United
States. Compliance requirements are not uniform and it takes a
considerable effort, much paperwork and many fees to meet and
keep up with these requirements. We do not solicit donations in
locations where we have not received written confirmation of
compliance. To SEND DONATIONS or determine the status of
compliance for any particular state visit www.gutenberg.org/donate.

While we cannot and do not solicit contributions from states where


we have not met the solicitation requirements, we know of no
prohibition against accepting unsolicited donations from donors in
such states who approach us with offers to donate.

International donations are gratefully accepted, but we cannot make


any statements concerning tax treatment of donations received from
outside the United States. U.S. laws alone swamp our small staff.

Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.

Section 5. General Information About


Project Gutenberg™ electronic works
Professor Michael S. Hart was the originator of the Project
Gutenberg™ concept of a library of electronic works that could be
freely shared with anyone. For forty years, he produced and
distributed Project Gutenberg™ eBooks with only a loose network of
volunteer support.
Project Gutenberg™ eBooks are often created from several printed
editions, all of which are confirmed as not protected by copyright in
the U.S. unless a copyright notice is included. Thus, we do not
necessarily keep eBooks in compliance with any particular paper
edition.

Most people start at our website which has the main PG search
facility: www.gutenberg.org.

This website includes information about Project Gutenberg™,


including how to make donations to the Project Gutenberg Literary
Archive Foundation, how to help produce our new eBooks, and how
to subscribe to our email newsletter to hear about new eBooks.

You might also like