Practical Common Lisp

Book description

...it has a fresh view on the language and the examples in the later chapters are usable in your day-to-day work as a programmer. — Frank Buss, Lisp Programmer and Slashdot Contributor

If you're interested in Lisp as it relates to Python or Perl, and want to learn through doing rather than watching, Practical Common Lisp is an excellent entry point. — Chris McAvoy, Chicago Python Users Group

Lisp is often thought of as an academic language, but it need not be. This is the first book that introduces Lisp as a language for the real world.

Practical Common Lisp presents a thorough introduction to Common Lisp, providing you with an overall understanding of the language features and how they work. Over a third of the book is devoted to practical examples such as the core of a spam filter and a web application for browsing MP3s and streaming them via the Shoutcast protocol to any standard MP3 client software (e.g., iTunes, XMMS, or WinAmp). In other "practical" chapters, author Peter Seibel demonstrates how to build a simple but flexible in-memory database, how to parse binary files, and how to build a unit test framework in 26 lines of code.

Table of contents

  1. Title Page
  2. Dedication
  3. Contents at a Glance
  4. Contents
  5. About the Author
  6. About the Technical Reviewer
  7. Acknowledgments
  8. Typographical Conventions
  9. CHAPTER 1 Introduction: Why Lisp?
    1. Why Lisp?
    2. Where It Began
    3. Who This Book Is For
  10. CHAPTER 2 Lather, Rinse, Repeat: A Tour of the REPL
    1. Choosing a Lisp Implementation
    2. Getting Up and Running with Lisp in a Box
    3. Free Your Mind: Interactive Programming
    4. Experimenting in the REPL
    5. "Hello, World," Lisp Style
    6. Saving Your Work
  11. CHAPTER 3 Practical: A Simple Database
    1. CDs and Records
    2. Filing CDs
    3. Looking at the Database Contents
    4. Improving the User Interaction
    5. Saving and Loading the Database
    6. Querying the Database
    7. Updating Existing Records—Another Use for WHERE
    8. Removing Duplication and Winning Big
    9. Wrapping Up
  12. CHAPTER 4 Syntax and Semantics
    1. What's with All the Parentheses?
    2. Breaking Open the Black Box
    3. S-expressions
    4. S-expressions As Lisp Forms
    5. Function Calls
    6. Special Operators
    7. Macros
    8. Truth, Falsehood, and Equality
    9. Formatting Lisp Code
  13. CHAPTER 5 Functions
    1. Defining New Functions
    2. Function Parameter Lists
    3. Optional Parameters
    4. Rest Parameters
    5. Keyword Parameters
    6. Mixing Different Parameter Types
    7. Function Return Values
    8. Functions As Data, a.k.a. Higher-Order Functions
    9. Anonymous Functions
  14. CHAPTER 6 Variables
    1. Variable Basics
    2. Lexical Variables and Closures
    3. Dynamic, a.k.a. Special, Variables
    4. Constants
    5. Assignment
    6. Generalized Assignment
    7. Other Ways to Modify Places
  15. CHAPTER 7 Macros: Standard Control Constructs
    1. WHEN and UNLESS
    2. COND
    3. AND, OR, and NOT
    4. Looping
    5. DOLIST and DOTIMES
    6. DO
    7. The Mighty LOOP
  16. CHAPTER 8 Macros: Defining Your Own
    1. The Story of Mac: A Just-So Story
    2. Macro Expansion Time vs. Runtime
    3. DEFMACRO
    4. A Sample Macro: do-primes
    5. Macro Parameters
    6. Generating the Expansion
    7. Plugging the Leaks
    8. Macro-Writing Macros
    9. Beyond Simple Macros
  17. CHAPTER 9 Practical: Building a Unit Test Framework
    1. Two First Tries
    2. Refactoring
    3. Fixing the Return Value
    4. Better Result Reporting
    5. An Abstraction Emerges
    6. A Hierarchy of Tests
    7. Wrapping Up
  18. CHAPTER 10 Numbers, Characters, and Strings
    1. Numbers
    2. Numeric Literals
    3. Basic Math
    4. Numeric Comparisons
    5. Higher Math
    6. Characters
    7. Character Comparisons
    8. Strings
    9. String Comparisons
  19. CHAPTER 11 Collections
    1. Vectors
    2. Subtypes of Vector
    3. Vectors As Sequences
    4. Sequence Iterating Functions
    5. Higher-Order Function Variants
    6. Whole Sequence Manipulations
    7. Sorting and Merging
    8. Subsequence Manipulations
    9. Sequence Predicates
    10. Sequence Mapping Functions
    11. Hash Tables
    12. Hash Table Iteration
  20. CHAPTER 12 They Called It LISP for a Reason: List Processing
    1. "There Is No List"
    2. Functional Programming and Lists
    3. "Destructive" Operations
    4. Combining Recycling with Shared Structure
    5. List-Manipulation Functions
    6. Mapping
    7. Other Structures
  21. CHAPTER 13 Beyond Lists: Other Uses for Cons Cells
    1. Trees
    2. Sets
    3. Lookup Tables: Alists and Plists
    4. DESTRUCTURING-BIND
  22. CHAPTER 14 Files and File I/O
    1. Reading File Data
    2. Reading Binary Data
    3. Bulk Reads
    4. File Output
    5. Closing Files
    6. Filenames
    7. How Pathnames Represent Filenames
    8. Constructing New Pathnames
    9. Two Representations of Directory Names
    10. Interacting with the File System
    11. Other Kinds of I/O
  23. CHAPTER 15 Practical: A Portable Pathname Library
    1. The API
    2. *FEATURES* and Read-Time Conditionalization
    3. Listing a Directory
    4. Testing a File's Existence
    5. Walking a Directory Tree
  24. CHAPTER 16 Object Reorientation: Generic Functions
    1. Generic Functions and Classes
    2. Generic Functions and Methods
    3. DEFGENERIC
    4. DEFMETHOD
    5. Method Combination
    6. The Standard Method Combination
    7. Other Method Combinations
    8. Multimethods
    9. To Be Continued . . .
  25. CHAPTER 17 Object Reorientation: Classes
    1. DEFCLASS
    2. Slot Specifiers
    3. Object Initialization
    4. Accessor Functions
    5. WITH-SLOTS and WITH-ACCESSORS
    6. Class-Allocated Slots
    7. Slots and Inheritance
    8. Multiple Inheritance
    9. Good Object-Oriented Design
  26. CHAPTER 18 A Few FORMAT Recipes
    1. The FORMAT Function
    2. FORMAT Directives
    3. Basic Formatting
    4. Character and Integer Directives
    5. Floating-Point Directives
    6. English-Language Directives
    7. Conditional Formatting
    8. Iteration
    9. Hop, Skip, Jump
    10. And More . . .
  27. CHAPTER 19 Beyond Exception Handling: Conditions and Restarts
    1. The Lisp Way
    2. Conditions
    3. Condition Handlers
    4. Restarts
    5. Providing Multiple Restarts
    6. Other Uses for Conditions
  28. CHAPTER 20 The Special Operators
    1. Controlling Evaluation
    2. Manipulating the Lexical Environment
    3. Local Flow of Control
    4. Unwinding the Stack
    5. Multiple Values
    6. EVAL-WHEN
    7. Other Special Operators
  29. CHAPTER 21 Programming in the Large: Packages and Symbols
    1. How the Reader Uses Packages
    2. A Bit of Package and Symbol Vocabulary
    3. Three Standard Packages
    4. Defining Your Own Packages
    5. Packaging Reusable Libraries
    6. Importing Individual Names
    7. Packaging Mechanics
    8. Package Gotchas
  30. CHAPTER 22 LOOP for Black Belts
    1. The Parts of a LOOP
    2. Iteration Control
    3. Counting Loops
    4. Looping Over Collections and Packages
    5. Equals-Then Iteration
    6. Local Variables
    7. Destructuring Variables
    8. Value Accumulation
    9. Unconditional Execution
    10. Conditional Execution
    11. Setting Up and Tearing Down
    12. Termination Tests
    13. Putting It All Together
  31. CHAPTER 23 Practical: A Spam Filter
    1. The Heart of a Spam Filter
    2. Training the Filter
    3. Per-Word Statistics
    4. Combining Probabilities
    5. Inverse Chi Square
    6. Training the Filter
    7. Testing the Filter
    8. A Couple of Utility Functions
    9. Analyzing the Results
    10. What's Next
  32. CHAPTER 24 Practical: Parsing Binary Files
    1. Binary Files
    2. Binary Format Basics
    3. Strings in Binary Files
    4. Composite Structures
    5. Designing the Macros
    6. Making the Dream a Reality
    7. Reading Binary Objects
    8. Writing Binary Objects
    9. Adding Inheritance and Tagged Structures
    10. Keeping Track of Inherited Slots
    11. Tagged Structures
    12. Primitive Binary Types
    13. The Current Object Stack
  33. CHAPTER 25 Practical: An ID3 Parser
    1. Structure of an ID3v2 Tag
    2. Defining a Package
    3. Integer Types
    4. String Types
    5. ID3 Tag Header
    6. ID3 Frames
    7. Detecting Tag Padding
    8. Supporting Multiple Versions of ID3
    9. Versioned Frame Base Classes
    10. Versioned Concrete Frame Classes
    11. What Frames Do You Actually Need?
    12. Text Information Frames
    13. Comment Frames
    14. Extracting Information from an ID3 Tag
  34. CHAPTER 26 Practical: Web Programming with AllegroServe
    1. A 30-Second Intro to Server-Side Web Programming
    2. AllegroServe
    3. Generating Dynamic Content with AllegroServe
    4. Generating HTML
    5. HTML Macros
    6. Query Parameters
    7. Cookies
    8. A Small Application Framework
    9. The Implementation
  35. CHAPTER 27 Practical: An MP3 Database
    1. The Database
    2. Defining a Schema
    3. Inserting Values
    4. Querying the Database
    5. Matching Functions
    6. Getting at the Results
    7. Other Database Operations
  36. CHAPTER 28 Practical: A Shoutcast Server
    1. The Shoutcast Protocol
    2. Song Sources
    3. Implementing Shoutcast
  37. CHAPTER 29 Practical: An MP3 Browser
    1. Playlists
    2. Playlists As Song Sources
    3. Manipulating the Playlist
    4. Query Parameter Types
    5. Boilerplate HTML
    6. The Browse Page
    7. The Playlist
    8. Finding a Playlist
    9. Running the App
  38. CHAPTER 30 Practical: An HTML Generation Library, the Interpreter
    1. Designing a Domain-Specific Language
    2. The FOO Language
    3. Character Escaping
    4. Indenting Printer
    5. HTML Processor Interface
    6. The Pretty Printer Backend
    7. The Basic Evaluation Rule
    8. What's Next?
  39. CHAPTER 31 Practical: An HTML Generation Library, the Compiler
    1. The Compiler
    2. FOO Special Operators
    3. FOO Macros
    4. The Public API
    5. The End of the Line
  40. CHAPTER 32 Conclusion: What's Next?
    1. Finding Lisp Libraries
    2. Interfacing with Other Languages
    3. Make It Work, Make It Right, Make It Fast
    4. Delivering Applications
    5. Where to Go Next
  41. INDEX

Product information

  • Title: Practical Common Lisp
  • Author(s): Peter Seibel
  • Release date: April 2005
  • Publisher(s): Apress
  • ISBN: 9781590592397