Basic LISP Techniques - Part1
Basic LISP Techniques - Part1
Basic LISP Techniques - Part1
0
Copyright
c 2011, Franz Inc. and David J. Cooper, Jr.
Foreword1
Computers, and the software applications that power them, permeate every facet of our
daily lives. From groceries to airline reservations to dental appointments, our reliance
on technology is all-encompassing. And, it’s not enough. Every day, our expectations of
technology and software increase:
• voice-activated laptops
The list is endless. Unfortunately, there is not an endless supply of programmers and
developers to satisfy our insatiable appetites for new features and gadgets. Every day,
hundreds of magazine and on-line articles focus on the time and people resources needed to
support future technological expectations. Further, the days of unlimited funding are over.
Investors want to see results, fast.
Common Lisp (CL) is one of the few languages and development options that can meet
these challenges. Powerful, flexible, changeable on the fly — increasingly, CL is playing
a leading role in areas with complex problem-solving demands. Engineers in the fields of
bioinformatics, scheduling, data mining, document management, B2B, and E-commerce
have all turned to CL to complete their applications on time and within budget. CL,
however, no longer just appropriate for the most complex problems. Applications of modest
complexity, but with demanding needs for fast development cycles and customization, are
also ideal candidates for CL.
Other languages have tried to mimic CL, with limited success. Perl, Python, Java,
C++, C# — they all incorporate some of the features that give Lisp its power, but their
implementations tend to be brittle.
The purpose of this book is to showcase the features that make CL so much better
than these imitators, and to give you a “quick-start” guide for using Common Lisp as a
development environment. If you are an experienced programmer in languages other than
Lisp, this guide gives you all the tools you need to begin writing Lisp applications. If you’ve
had some exposure to Lisp in the past, this guide will help refresh those memories and shed
some new light on CL for you.
1
this Foreword was authored by Franz Inc.
iii
iv
But be careful, Lisp can be addicting! This is why many Fortune 500 companies will
use nothing else on their 24/7, cutting-edge, mission-critical applications. After reading
this book, trying our software, and experiencing a 3 to 10 times increase in productivity,
we believe you will feel the same way.
Contents
1 Introduction 1
1.1 The Past, Present, and Future of Common Lisp . . . . . . . . . . . . . . . . 1
1.1.1 Lisp Yesterday . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1.2 Lisp Today . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1.3 Lisp Tomorrow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 Convergence of Hardware and Software . . . . . . . . . . . . . . . . . . . . 3
1.3 The CL Model of Computation . . . . . . . . . . . . . . . . . . . . . . . . . 3
v
vi CONTENTS
3 The CL Language 19
3.1 Overview of CL and its Syntax . . . . . . . . . . . . . . . . . . . . . . . . . 19
3.1.1 Evaluation of Arguments to a Function . . . . . . . . . . . . . . . . 21
3.1.2 Lisp Syntax Simplicity . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.1.3 Turning Off Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.1.4 Fundamental CL Data Types . . . . . . . . . . . . . . . . . . . . . . 22
3.1.5 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
3.1.6 Global and Local Variables . . . . . . . . . . . . . . . . . . . . . . . 25
3.2 The List as a Data Structure . . . . . . . . . . . . . . . . . . . . . . . . . . 27
3.2.1 Accessing the Elements of a List . . . . . . . . . . . . . . . . . . . . 28
3.2.2 The “Rest” of the Story . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.2.3 The Empty List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.2.4 Are You a List? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3.2.5 The conditional If . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
3.2.6 Length of a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
3.2.7 Member of a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.2.8 Getting Part of a List . . . . . . . . . . . . . . . . . . . . . . . . . . 32
3.2.9 Appending Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
3.2.10 Adding Elements to a List . . . . . . . . . . . . . . . . . . . . . . . . 33
3.2.11 Removing Elements from a List . . . . . . . . . . . . . . . . . . . . . 33
3.2.12 Sorting Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.2.13 Treating a List as a Set . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.2.14 Mapping a Function to a List . . . . . . . . . . . . . . . . . . . . . . 35
3.2.15 Property Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
3.3 Control of Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
3.3.1 If . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
3.3.2 When . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
3.3.3 Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
3.3.4 Cond . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
3.3.5 Case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
3.3.6 Iteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
3.4 Functions as Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
3.4.1 Named Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
3.4.2 Functional Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . 40
3.4.3 Anonymous Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 40
3.4.4 Optional Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
3.4.5 Keyword Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
3.5 Input, Output, Streams, and Strings . . . . . . . . . . . . . . . . . . . . . . 42
3.5.1 Read . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
3.5.2 Print and Prin1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
3.5.3 Princ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
3.5.4 Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
CONTENTS vii
3.5.5 Pathnames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
3.5.6 File Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . 44
3.6 Hash Tables, Arrays, Structures, and Classes . . . . . . . . . . . . . . . . . 45
3.6.1 Hash Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
3.6.2 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
3.6.3 Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
3.6.4 Classes and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
3.7 Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
3.7.1 Importing and Exporting Symbols . . . . . . . . . . . . . . . . . . . 49
3.7.2 The Keyword Package . . . . . . . . . . . . . . . . . . . . . . . . . . 49
3.8 Common Stumbling Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
3.8.1 Quotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
3.8.2 Function Argument Lists . . . . . . . . . . . . . . . . . . . . . . . . 50
3.8.3 Symbols vs. Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
3.8.4 Equality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
3.8.5 Distinguishing Macros from Functions . . . . . . . . . . . . . . . . . 53
3.8.6 Operations that cons . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
4 Interfaces 55
4.1 Interfacing with the Operating System . . . . . . . . . . . . . . . . . . . . . 55
4.2 Foreign Function Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
4.3 Interfacing with Corba . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
4.4 Custom Socket Connections . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
4.5 Interfacing with Windows (COM, DLL, DDE) . . . . . . . . . . . . . . . . . 58
4.6 Code Generation into Other Languages . . . . . . . . . . . . . . . . . . . . 58
4.7 Multiprocessing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
4.7.1 Starting a Background Process . . . . . . . . . . . . . . . . . . . . . 58
4.7.2 Concurrency Control . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
4.8 Database Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
4.8.1 ODBC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
4.8.2 MySQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
4.9 World Wide Web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
4.9.1 Server and HTML Generation . . . . . . . . . . . . . . . . . . . . . . 62
4.9.2 Client and HTML Parsing . . . . . . . . . . . . . . . . . . . . . . . . 63
4.10 Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
4.11 Email . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
4.11.1 Sending Mail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
4.11.2 Retrieving Mail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
5 Squeakymail 67
5.1 Overview of Squeakymail . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
5.2 Fetching and Scoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
5.2.1 Tokenizing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
5.2.2 Scoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
5.3 Browsing and Classifying . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
viii CONTENTS
B Bibliography 83
C Emacs Customization 85
C.1 Lisp Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
C.2 Making Your Own Keychords . . . . . . . . . . . . . . . . . . . . . . . . . . 85
C.3 Keyboard Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
D Afterword 87
D.1 About This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
D.2 Acknowledgements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
D.3 About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Chapter 1
Introduction
1
2 CHAPTER 1. INTRODUCTION
Paul Graham outlines ANSI CL in his 1996 book ANSI Common Lisp, also listed in
Appendix B.
Official language standardization is important, because it protects developers from be-
coming saddled with legacy applications as new versions of a language are implemented.
For example, this lack of standardization has been a continuing problem for Perl developers.
Since each implementation of Perl defines the behavior of the language, it is not uncommon
to have applications that require outdated versions of Perl, making it nearly impossible to
use in a mission-critical commercial environment.
• Dynamic Typing: In CL, values have types, but variables do not. This means that
you don’t have to declare variables, or placeholders, ahead of time to be of a particular
type; you can simply create them or modify them on the fly1 .
• Dynamic Redefinition: You can add new operations and functionality to the running
CL process without the need for downtime. In fact, a program can be running in one
thread, while parts of the code are redefined in another thread. Moreover, as soon as
the redefinitions are finished, the application will use the new code and is effectively
modified while running. This feature is especially useful for server applications that
cannot afford downtimes – you can patch and repair them while they are running.
• Standard Features: CL contains many built-in features and data structures that are
non-standard in other languages. Features such as full-blown symbol tables and pack-
age systems, hash tables, list structures, and a comprehensive object system are sev-
eral “automatic” features which would require significant developer time to recreate
in other languages.
tables
hash, 45
test-expression forms
for cond, 37
threads, 1
toplevel, 19
turning off evaluation, 22
Typing
Dynamic, 22
union, 34
Unix, 44
variables
global, 25
special, 25
webserver, 62
With-open-file, 45