Lisp has been hailed as the world's most powerful programming language, but its cryptic syntax and academic reputation can be enough to scare off even experienced programmers. Those dark days are finally over—Land of Lisp brings the power of functional programming to the people!With his brilliantly quirky comics and out-of-this-world games, longtime Lisper Conrad Barski teaches you the mysteries of Common Lisp. You'll start with the basics, like list manipulation, I/O, and recursion, then move on to more complex topics like macros, higher order programming, and domain-specific languages. Then, when your brain overheats, you can kick back with an action-packed comic book interlude!Along the way you'll create (and play) games like Wizard Adventure, a text adventure with a whiskey-soaked twist, and Grand Theft Wumpus, the most violent version of Hunt the Wumpus the world has ever seen.You'll learn to: Master the quirks of Lisp's syntax and semantics Write concise and elegant functional programs Use macros, create domain-specific languages, and learn other advanced Lisp techniques Create your own web server, and use it to play browser-based games Put your Lisp skills to the test by writing brain-melting games like Dice of Doom and Orc Battle With Land of Lisp, the power of functional programming is yours to wield.
Author(s): Conrad Barski M.D.
Edition: 1
Publisher: No Starch Press
Year: 2010
Language: English
Pages: 508
Tags: Библиотека;Компьютерная литература;LISP / Clojure;
Copyright......Page 6
Acknowledgments......Page 19
Introduction......Page 21
What Makes Lisp So Cool and Unusual?......Page 22
If Lisp Is So Great, Why Don’t More People Use It?......Page 23
Where Did Lisp Come From?......Page 24
Where Does Lisp Get Its Power?......Page 30
Section I: Lisp Is Power
......Page 33
Lisp Dialects......Page 35
A Tale of Two Lisps......Page 36
ANSI Common Lisp......Page 37
Installing CLISP......Page 38
What You’ve Learned......Page 39
The Guess-My-Number Game......Page 41
An Alternative Global Variable Definition Function......Page 43
Basic Lisp Etiquette......Page 44
Defining the guess-my-number Function......Page 45
Defining the smaller and bigger Functions......Page 47
Defining Local Variables in Lisp......Page 48
Defining Local Functions in Lisp......Page 49
What You’ve Learned......Page 50
Syntax and Semantics......Page 51
The Building Blocks of Lisp Syntax......Page 52
Symbols......Page 53
Numbers......Page 54
How Lisp Distinguishes Between Code and Data......Page 55
Code Mode......Page 56
Lists in Lisp......Page 57
List Functions......Page 58
Nested Lists......Page 61
What You’ve Learned......Page 65
Section II: Lisp Is Symmetry
......Page 67
The Symmetry of nil and ()......Page 69
Empty Equals False......Page 70
The Four Disguises of ()......Page 71
One Thing at a Time with if......Page 72
Going Beyond if: The when and unless Alternatives......Page 75
The Command That Does It All: cond......Page 76
Branching with case......Page 77
Using the Stealth Conditionals and and or......Page 78
Using Functions That Return More than Just the Truth......Page 80
Comparing Stuff: eq, equal, and More......Page 82
What You’ve Learned......Page 85
5: Building a Text Game Engine
......Page 87
Our Game World......Page 88
Basic Requirements......Page 89
Describing the Scenery with an Association List......Page 90
Describing the Location......Page 91
Describing the Paths......Page 92
Describing Multiple Paths at Once......Page 93
Listing Visible Objects......Page 97
Describing Visible Objects......Page 98
Describing It All......Page 99
Walking Around in Our World......Page 101
Picking Up Objects......Page 102
Checking Our Inventory......Page 103
What You’ve Learned......Page 104
6: Interacting with the World: Reading and Printing in Lisp
......Page 105
Printing to the Screen......Page 106
Saying Hello to the User......Page 107
Starting with print and read......Page 108
Reading and Printing Stuff the Way Humans Like It......Page 110
The Symmetry Between Code and Data in Lisp......Page 111
Adding a Custom Interface to Our Game Engine......Page 112
Setting Up a Custom REPL......Page 113
Writing a Custom read Function......Page 114
Writing a game-print Function......Page 116
Trying Out Our Fancy New Game Interface......Page 119
What You’ve Learned......Page 121
What lambda Does......Page 123
Why lambda Is So Important......Page 125
What You’ve Learned......Page 126
Exotic Lists......Page 127
Dotted Lists......Page 128
Pairs......Page 129
Circular Lists......Page 130
Association Lists......Page 131
Visualizing Tree-like Data......Page 133
Creating a Graph......Page 134
Generating the DOT Information......Page 135
Turning the DOT File into a Picture......Page 140
Creating a Picture of Our Graph......Page 143
Creating Undirected Graphs......Page 144
What You’ve Learned......Page 147
8: This Ain’t Your Daddy’s Wumpus
......Page 149
The Grand Theft Wumpus Game......Page 151
Generating Random Edges......Page 155
Looping with the loop Command......Page 156
Preventing Islands......Page 157
Building the Final Edges for Congestion City......Page 159
Building the Nodes for Congestion City......Page 162
Initializing a New Game of Grand Theft Wumpus......Page 164
Drawing a Map of Our City......Page 165
Drawing a City from Partial Knowledge......Page 166
Walking Around Town......Page 168
Let’s Hunt Some Wumpus!......Page 169
What You’ve Learned......Page 172
Arrays......Page 173
Using a Generic Setter......Page 174
Arrays vs. Lists......Page 176
Working with Hash Tables......Page 177
Returning Multiple Values......Page 179
Hash Table Performance......Page 180
A Faster Grand Theft Wumpus Using Hash Tables......Page 181
Working with Structures......Page 183
When to Use Structures......Page 185
Working with Sequences......Page 186
Creating Your Own Generic Functions with Type Predicates......Page 190
The Orc Battle Game......Page 192
Global Variables for the Player and Monsters......Page 193
Main Game Functions......Page 194
Player Management Functions......Page 195
Helper Functions for Player Attacks......Page 197
Monster Management Functions......Page 198
The Monsters......Page 199
To Battle!......Page 207
What You’ve Learned......Page 209
Section III: Lisp Is Hacking
......Page 211
loop and format: The Seedy Underbelly of Lisp......Page 213
The loop Macro......Page 215
Some loop Tricks......Page 216
Using loop to Evolve!......Page 222
Growing Plants in Our World......Page 224
Creating Animals......Page 225
Drawing Our World......Page 232
Creating a User Interface......Page 233
Let’s Watch Some Evolution!......Page 234
Explaining the Evolution......Page 238
What You’ve Learned......Page 239
Anatomy of the format Function......Page 241
The Control String Parameter......Page 242
Control Sequences for Printing Lisp Values......Page 243
Control Sequences for Formatting Integers......Page 245
Printing Multiple Lines of Output......Page 246
Justifying Output......Page 248
Iterating Through Lists Using Control Sequences......Page 251
A Crazy Formatting Trick for Creating Pretty Tables of Data......Page 252
Attack of the Robots!......Page 253
What You’ve Learned......Page 255
12: Working with Streams
......Page 257
Streams by Direction......Page 258
Working with Files......Page 262
Working with Sockets......Page 264
Socket Addresses......Page 265
Sending a Message over a Socket......Page 266
Tidying Up After Ourselves......Page 268
Sending Streams to Functions......Page 269
Reading and Debugging......Page 270
What You’ve Learned......Page 271
Error Handling in Common Lisp......Page 273
Creating Custom Conditions......Page 274
Protecting Resources Against Unexpected Conditions......Page 275
How a Web Server Works......Page 276
Request Parameters......Page 278
Parsing the Request Header......Page 281
Testing get-header with a String Stream......Page 282
Our Grand Finale: The serve Function!......Page 283
Testing the Request Handler......Page 285
Launching the Website......Page 286
What You’ve Learned......Page 287
"Functional Programming Is Beautiful"
......Page 289
Section IV: Lisp Is Science
......Page 309
14: Ramping Lisp Up a Notch with Functional Programming
......Page 311
What Is Functional Programming?......Page 312
Anatomy of a Program Written in the Functional Style......Page 315
Code Composition with Imperative Code......Page 318
Using the Functional Style......Page 319
Why Functional Programming Is Crazy......Page 320
Functional Programs Are More Compact......Page 321
What You’ve Learned......Page 322
15: Dice of Doom, a Game Written in the Functional Style
......Page 323
A Sample Game of Dice of Doom......Page 324
Defining Some Global Variables......Page 326
Representing the Game Board......Page 327
Decoupling Dice of Doom’s Rules from the Rest of the Game......Page 329
Generating a Game Tree......Page 331
Calculating Passing Moves......Page 332
Calculating Attacking Moves......Page 333
Finding the Neighbors......Page 334
Attacking......Page 335
Reinforcements......Page 336
Trying Out Our New game-tree Function......Page 337
Playing Dice of Doom Against Another Human......Page 338
Creating an Intelligent Computer Opponent......Page 341
Turning Minimax into Actual Code......Page 343
Creating a Game Loop with an AI Player......Page 344
Playing Our First Human vs. Computer Game......Page 345
Closures......Page 346
Memoization......Page 348
Tail Call Optimization......Page 351
A Sample Game on the 3-by-3 Board......Page 354
What You’ve Learned......Page 356
16: The Magic of Lisp Macros
......Page 359
A Simple Lisp Macro......Page 360
Macro Expansion......Page 361
How Macros Are Transformed......Page 362
More Complex Macros......Page 365
A Macro for Splitting Lists......Page 366
Avoiding Repeated Execution in Macros......Page 367
Avoiding Variable Capture......Page 368
A Recursion Macro......Page 370
Macros: Dangers and Alternatives......Page 372
What You’ve Learned......Page 373
What Is a Domain?......Page 375
Writing SVG Files......Page 376
Creating XML and HTML with the tag Macro......Page 377
Creating SVG-Specific Macros and Functions......Page 381
Building a More Complicated SVG Example......Page 382
Creating Custom Game Commands for Wizard’s Adventure Game......Page 385
Creating New Game Commands by Hand......Page 386
Let’s Try the Completed Wizard’s Adventure Game!......Page 391
What You’ve Learned......Page 393
18: Lazy Programming
......Page 395
Adding Lazy Evaluation to Lisp......Page 396
Creating the lazy and force Commands......Page 398
Creating a Lazy Lists Library......Page 400
Converting Between Regular Lists and Lazy Lists......Page 401
Mapping and Searching Across Lazy Lists......Page 403
Dice of Doom, Version 2......Page 404
Trimming the Game Tree......Page 407
Winning by a Lot vs. Winning by a Little......Page 409
Alpha Beta Pruning......Page 413
What You’ve Learned......Page 420
19: Creating a Graphical, Web-Based Version of Dice of Doom
......Page 421
Drawing the Game Board Using the SVG Format......Page 422
Drawing a Die......Page 423
Drawing a Tile......Page 425
Drawing the Board......Page 426
Writing Our Web Request Handler......Page 428
Limitations of Our Game Web Server......Page 429
Handling the Human Player......Page 430
Drawing the SVG Game Board from Within the HTML......Page 432
Playing Version 3 of Dice of Doom......Page 433
What You’ve Learned......Page 435
Increasing the Number of Players......Page 437
Rolling the Dice......Page 438
Building Chance Nodes......Page 439
Calling the Dice Rolling Code from Our Game Engine......Page 440
Updating the AI......Page 442
Improving the Dice of Doom Reinforcement Rules......Page 443
Conclusion......Page 445
Epilogue......Page 449
FUNCTIONAL GUILD CRUISER......Page 461
MACRO GUILD MELEE FIGHTERS......Page 463
RESTART GUILD ARMORED FIGHTER......Page 464
GENERIC SETTER GUILD SUPPLY SHIP......Page 467
DSL GUILD HOT RODS......Page 470
CLOS GUILD BATTLESHIP......Page 471
THE CONTINUATION GUILD ROCKET PODS......Page 474
BREVITY GUILD MICRO FIGHTER......Page 479
MULTICORE GUILD FORMATION FIGHTERS......Page 481
THE LAZY GUILD FRIGATE......Page 482
Index
......Page 485