You are previewing Land of Lisp.
O'Reilly logo
Land of Lisp

Book Description

Lisp is a uniquely powerful programming language that, despite its academic reputation, is actually very practical. Land of Lisp brings the language into the real world, teaching readers Lisp by showing them how to write several complete Lisp-based games, including a text adventure, an evolution simulation, and a robot battle. While building these games, readers learn the core concepts of Lisp programming, such as recursion, input/output, object-oriented programming, and macros. And thanks to Lisp's powerful syntax, the example code is short and easy to understand. The book is filled with the author's brilliant Lisp cartoons, which are sure to appeal to many Lisp fans and, in the tradition of all No Starch Press titles, make learning more fun.

Table of Contents

  1. Land of Lisp
    1. Acknowledgments
    2. Introduction
      1. What Makes Lisp So Cool and Unusual?
      2. If Lisp Is So Great, Why Don't More People Use It?
      3. Where Did Lisp Come From?
      4. Where Does Lisp Get Its Power?
    3. I. Lisp is Power
      1. 1. Getting Started with Lisp
        1. Lisp Dialects
          1. A Tale of Two Lisps
          2. Up-and-Coming Lisps
          3. Lisp Dialects Used for Scripting
          4. ANSI Common Lisp
        2. Getting Started with CLISP
          1. Installing CLISP
          2. Starting Up CLISP
        3. What You've Learned
      2. 2. Creating Your First Lisp Program
        1. The Guess-My-Number Game
        2. Defining Global Variables in Lisp
          1. Defining the small and big Variables
          2. An Alternative Global Variable Definition Function
        3. Basic Lisp Etiquette
        4. Defining Global Functions in Lisp
          1. Defining the guess-my-number Function
          2. Defining the smaller and bigger Functions
          3. Defining the start-over Function
        5. Defining Local Variables in Lisp
        6. Defining Local Functions in Lisp
        7. What You've Learned
      3. 3. Exploring the Syntax of Lisp Code
        1. Syntax and Semantics
        2. The Building Blocks of Lisp Syntax
          1. Symbols
          2. Numbers
          3. Strings
        3. How Lisp Distinguishes Between Code and Data
          1. Code Mode
          2. Data Mode
        4. Lists in Lisp
          1. Cons Cells
          2. List Functions
            1. The cons Function
            2. The car and cdr Functions
            3. The list Function
          3. Nested Lists
        5. What You've Learned
    4. II. Lisp is Symmetry
      1. 4. Making Decisions with Conditions
        1. The Symmetry of nil and ()
          1. Empty Equals False
          2. The Four Disguises of ()
        2. The Conditionals: if and Beyond
          1. One Thing at a Time with if
          2. Going Beyond if: The when and unless Alternatives
          3. The Command That Does It All: cond
          4. Branching with case
        3. Cool Tricks with Conditions
          1. Using the Stealth Conditionals and and or
          2. Using Functions That Return More than Just the Truth
        4. Comparing Stuff: eq, equal, and More
        5. What You've Learned
      2. 5. Building a Text Game Engine
        1. The Wizard's Adventure Game
          1. Our Game World
          2. Basic Requirements
        2. Describing the Scenery with an Association List
        3. Describing the Location
        4. Describing the Paths
          1. How Quasiquoting Works
          2. Describing Multiple Paths at Once
            1. Finding the Relevant Edges
            2. Converting the Edges to Descriptions
            3. Joining the Descriptions
        5. Describing Objects at a Specific Location
          1. Listing Visible Objects
          2. Describing Visible Objects
        6. Describing It All
        7. Walking Around in Our World
        8. Picking Up Objects
        9. Checking Our Inventory
        10. What You've Learned
      3. 6. Interacting with the World: Reading and Printing in Lisp
        1. Printing and Reading Text
          1. Printing to the Screen
          2. Saying Hello to the User
          3. Starting with print and read
          4. Reading and Printing Stuff the Way Humans Like It
        2. The Symmetry Between Code and Data in Lisp
        3. Adding a Custom Interface to Our Game Engine
          1. Setting Up a Custom REPL
          2. Writing a Custom read Function
          3. Writing a game-eval Function
          4. Writing a game-print Function
        4. Trying Out Our Fancy New Game Interface
        5. The Dangers of read and eval
        6. What You've Learned
      4. 6.5. lambda: A Function So Important It Deserves Its Own Chapter
        1. What lambda Does
        2. Why lambda Is So Important
        3. What You've Learned
      5. 7. Going Beyond Basic Lists
        1. Exotic Lists
          1. Dotted Lists
          2. Pairs
          3. Circular Lists
          4. Association Lists
        2. Coping with Complicated Data
          1. Visualizing Tree-like Data
          2. Visualizing Graphs
        3. Creating a Graph
          1. Generating the DOT Information
            1. Converting Node Identifiers
            2. Adding Labels to Graph Nodes
            3. Generating the DOT Information for the Nodes
            4. Converting Edges into DOT Format
            5. Generating All the DOT Data
          2. Turning the DOT File into a Picture
            1. Using Thunks
            2. Writing to a File
            3. Creating a Stream
            4. Understanding Keyword Parameters
            5. Capturing the Console Output
          3. Creating a Picture of Our Graph
        4. Creating Undirected Graphs
        5. What You've Learned
      6. 8. This Ain't Your Daddy's Wumpus
        1. The Grand Theft Wumpus Game
        2. Defining the Edges of Congestion City
          1. Generating Random Edges
          2. Looping with the loop Command
          3. Preventing Islands
          4. Building the Final Edges for Congestion City
            1. The make-city-edges Function
            2. The edges-to-alist Function
            3. The add-cops Function
        3. Building the Nodes for Congestion City
        4. Initializing a New Game of Grand Theft Wumpus
        5. Drawing a Map of Our City
          1. Drawing a City from Partial Knowledge
            1. Known Nodes
            2. Known Edges
            3. The mapcan Function
            4. Drawing Only the Known Parts of the City
          2. Walking Around Town
        6. Let's Hunt Some Wumpus!
        7. What You've Learned
      7. 9. Advanced Datatypes and Generic Programming
        1. Arrays
          1. Working with Arrays
          2. Using a Generic Setter
          3. Arrays vs. Lists
        2. Hash Tables
          1. Working with Hash Tables
          2. Returning Multiple Values
          3. Hash Table Performance
          4. A Faster Grand Theft Wumpus Using Hash Tables
        3. Common Lisp Structures
          1. Working with Structures
          2. When to Use Structures
        4. Handling Data in a Generic Way
          1. Working with Sequences
            1. Sequence Functions for Searching
            2. Sequence Functions for Iterating Across a Sequence
            3. Two More Important Sequence Functions
          2. Creating Your Own Generic Functions with Type Predicates
        5. The Orc Battle Game
          1. Global Variables for the Player and Monsters
          2. Main Game Functions
          3. Player Management Functions
          4. Helper Functions for Player Attacks
          5. Monster Management Functions
          6. The Monsters
            1. The Generic Monster
            2. The Wicked Orc
            3. The Malicious Hydra
            4. The Slimy Slime Mold
            5. The Cunning Brigand
          7. To Battle!
        6. What You've Learned
    5. III. Lisp is Hacking
      1. 10. Looping with the loop Command
        1. The loop Macro
          1. Some loop Tricks
            1. Counting from a Starting Point to an Ending Point
            2. Iterating Through Values in a List
            3. doing Stuff in a Loop
            4. Doing Stuff Under Certain Conditions
            5. Breaking out of a Loop Early
            6. Collecting a List of Values
            7. Using Multiple for Clauses
          2. Everything You Ever Wanted to Know About loop
        2. Using loop to Evolve!
          1. Growing Plants in Our World
          2. Creating Animals
            1. Anatomy of an Animal
            2. Handling Animal Motion
            3. Handling Animal Turning
            4. Handling Animal Eating
            5. Handling Animal Reproduction
          3. Simulating a Day in Our World
          4. Drawing Our World
          5. Creating a User Interface
          6. Let's Watch Some Evolution!
          7. Explaining the Evolution
        3. What You've Learned
      2. 11. Printing Text with the format Function
        1. Anatomy of the format Function
          1. The Destination Parameter
          2. The Control String Parameter
          3. Value Parameters
        2. Control Sequences for Printing Lisp Values
        3. Control Sequences for Formatting Numbers
          1. Control Sequences for Formatting Integers
          2. Control Sequences for Formatting Floating-Point Numbers
        4. Printing Multiple Lines of Output
        5. Justifying Output
        6. Iterating Through Lists Using Control Sequences
        7. A Crazy Formatting Trick for Creating Pretty Tables of Data
        8. Attack of the Robots!
        9. What You've Learned
      3. 12. Working with Streams
        1. Types of Streams
          1. Streams by Type of Resource
          2. Streams by Direction
            1. Output Streams
            2. Input Streams
        2. Working with Files
        3. Working with Sockets
          1. Socket Addresses
          2. Socket Connections
          3. Sending a Message over a Socket
          4. Tidying Up After Ourselves
        4. String Streams: The Oddball Type
          1. Sending Streams to Functions
          2. Working with Long Strings
          3. Reading and Debugging
        5. What You've Learned
      4. 13. Let's Create a Web Server!
        1. Error Handling in Common Lisp
          1. Signaling a Condition
          2. Creating Custom Conditions
          3. Intercepting Conditions
          4. Protecting Resources Against Unexpected Conditions
        2. Writing a Web Server from Scratch
          1. How a Web Server Works
          2. Request Parameters
            1. Request Parameters for GET Requests
            2. Decoding the Values of Request Parameters
            3. Decoding Lists of Request Parameters
          3. Parsing the Request Header
          4. Testing get-header with a String Stream
          5. Parsing the Request Body
          6. Our Grand Finale: The serve Function!
        3. Building a Dynamic Website
          1. Testing the Request Handler
          2. Launching the Website
        4. What You've Learned
      5. 13.5. Functional Programming Is Beautiful
    6. IV. Lisp is Science
      1. 14. Ramping Lisp Up a Notch with Functional Programming
        1. What Is Functional Programming?
        2. Anatomy of a Program Written in the Functional Style
        3. Higher-Order Programming
          1. Code Composition with Imperative Code
          2. Using the Functional Style
          3. Higher-Order Programming to the Rescue
        4. Why Functional Programming Is Crazy
        5. Why Functional Programming Is Fantastic
          1. Functional Programming Reduces Bugs
          2. Functional Programs Are More Compact
          3. Functional Code Is More Elegant
        6. What You've Learned
      2. 15. Dice of Doom, a Game Written in the Functional Style
        1. The Rules of Dice of Doom
        2. A Sample Game of Dice of Doom
        3. Implementing Dice of Doom, Version 1
          1. Defining Some Global Variables
          2. Representing the Game Board
          3. Decoupling Dice of Doom's Rules from the Rest of the Game
          4. Generating a Game Tree
          5. Calculating Passing Moves
          6. Calculating Attacking Moves
          7. Finding the Neighbors
          8. Attacking
          9. Reinforcements
          10. Trying Out Our New game-tree Function
          11. Playing Dice of Doom Against Another Human
            1. The Main Loop
            2. Giving Information About the State of the Game
            3. Handling Input from Human Players
            4. Determining the Winner
            5. Trying Out the Human vs. Human Version of Dice of Doom
        4. Creating an Intelligent Computer Opponent
          1. The Minimax Algorithm
          2. Turning Minimax into Actual Code
          3. Creating a Game Loop with an AI Player
          4. Playing Our First Human vs. Computer Game
        5. Making Dice of Doom Faster
          1. Closures
          2. Memoization
            1. Memoizing the neighbors Function
            2. Memoizing the Game Tree
            3. Memoizing the rate-position Function
          3. Tail Call Optimization
            1. Support for Tail Calls in Common Lisp
            2. Tail Call Optimization in Dice of Doom
          4. A Sample Game on the 3-by-3 Board
        6. What You've Learned
      3. 16. The Magic of Lisp Macros
        1. A Simple Lisp Macro
          1. Macro Expansion
          2. How Macros Are Transformed
          3. Using the Simple Macro
        2. More Complex Macros
          1. A Macro for Splitting Lists
          2. Avoiding Repeated Execution in Macros
          3. Avoiding Variable Capture
          4. A Recursion Macro
        3. Macros: Dangers and Alternatives
        4. What You've Learned
      4. 17. Domain-Specific Languages
        1. What Is a Domain?
        2. Writing SVG Files
          1. Creating XML and HTML with the tag Macro
            1. Writing a Macro Helper Function
            2. Creating the tag Macro
            3. Using the tag Macro to Generate HTML
          2. Creating SVG-Specific Macros and Functions
          3. Building a More Complicated SVG Example
        3. Creating Custom Game Commands for Wizard's Adventure Game
          1. Creating New Game Commands by Hand
            1. A Command for Welding
            2. A Command for Dunking
            3. The game-action Macro
          2. Let's Try the Completed Wizard's Adventure Game!
        4. What You've Learned
      5. 18. Lazy Programming
        1. Adding Lazy Evaluation to Lisp
          1. Creating the lazy and force Commands
          2. Creating a Lazy Lists Library
          3. Converting Between Regular Lists and Lazy Lists
          4. Mapping and Searching Across Lazy Lists
        2. Dice of Doom, Version 2
        3. Making Our AI Work on Larger Game Boards
          1. Trimming the Game Tree
          2. Applying Heuristics
          3. Winning by a Lot vs. Winning by a Little
          4. Alpha Beta Pruning
        4. What You've Learned
      6. 19. Creating a Graphical, Web-Based Version of Dice of Doom
        1. Drawing the Game Board Using the SVG Format
          1. Drawing a Die
          2. Drawing a Tile
          3. Drawing the Board
        2. Building the Web Server Interface
          1. Writing Our Web Request Handler
          2. Limitations of Our Game Web Server
          3. Initializing a New Game
          4. Announcing a Winner
          5. Handling the Human Player
          6. Handling the Computer Player
          7. Drawing the SVG Game Board from Within the HTML
        3. Playing Version 3 of Dice of Doom
        4. What You've Learned
      7. 20. Making Dice of Doom More Fun
        1. Increasing the Number of Players
        2. Rolling the Dice
          1. Building Chance Nodes
          2. Doing the Actual Dice Rolling
          3. Calling the Dice Rolling Code from Our Game Engine
          4. Updating the AI
        3. Improving the Dice of Doom Reinforcement Rules
        4. Conclusion
      8. A. Epilogue
        1. Functional Guild Cruiser
          1. Synopsis
          2. How It Kills Bugs
          3. Explanation
          4. Weakness
        2. Macro Guild Melee Fighters
          1. Synopsis
          2. How It Kills Bugs
          3. Explanation
          4. Weakness
        3. Restart Guild Armored Fighter
          1. Synopsis
          2. How It Kills Bugs
          3. Explanation
          4. Weakness
        4. Generic Setter Guild Supply Ship
          1. Synopsis
          2. How It Kills Bugs
          3. Explanation
          4. Weakness
        5. DSL Guild Hot Rods
          1. Synopsis
          2. Explanation
          3. Weakness
        6. CLOS Guild Battleship
          1. Synopsis
          2. How It Kills Bugs
          3. Explanation
          4. Explanation
          5. Weakness
        7. The Continuation Guild Rocket Pods
          1. Synopsis
          2. How It Kills Bugs
          3. Explanation
          4. Weakness
        8. Brevity Guild Micro Fighter
          1. Synopsis
          2. How It Kills Bugs
          3. Explanation
          4. Weakness
        9. Multicore Guild Formation Fighters
          1. Synopsis
          2. How It Fights Bugs
          3. Explanation
          4. Weakness
        10. The Lazy Guild Frigate
          1. Lisp Dialect
          2. Synopsis
          3. How It Kills Bugs
          4. Explanation
          5. Weakness
    7. Index
    8. About the Author
    9. Updates