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