You are previewing Realm of Racket.

Realm of Racket

Cover of Realm of Racket by Conrad Barski M.D.... Published by No Starch Press
  1. Dedication
  2. Acknowledgments
  3. Preface (Hello World)
    1. Why Would I Want to Learn About Racket?
    2. Who Should Read This Book?
    3. What Teaching Approach Is Used?
    4. Can I Skip Chapters?
    5. Anything Else I Should Know?
  4. Introduction (Open Paren)
    1. (.1 What Makes Lisp So Cool and Unusual?
    2. (.2 Where Did Lisp Come From?
    3. (.3 What Does Lisp Look Like?
    4. (.4 Where Does Racket Come From?
    5. (.5 What Is This Book About?
    6. <code xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="literal">Halt</code>&#8212;Chapter Checkpoint—Chapter Checkpoint
  5. 1. (Getting Started)
    1. 1.1 Readying Racket
    2. 1.2 Interacting with Racket
    3. <code xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="literal">Raise</code>&#8212;Chapter Checkpoint—Chapter Checkpoint
  6. 2. (A First Racket Program)
    1. 2.1 The Guess My Number Game
    2. 2.2 Defining Variables
    3. 2.3 Basic Racket Etiquette
    4. 2.4 Defining Functions in Racket
      1. A Function for Guessing
      2. Functions for Closing In
      3. The Main Function
    5. <code xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="literal">Resume</code>&#8212;Chapter Checkpoint—Chapter Checkpoint
  7. 3. (Basics of Racket)
    1. 3.1 Syntax and Semantics
    2. 3.2 The Building Blocks of Racket Syntax
    3. 3.3 The Building Blocks of Racket Semantics
      1. Booleans
      2. Symbols
      3. Numbers
      4. Strings
    4. 3.4 Lists in Racket
      1. CONS Cells
      2. Functions for CONS Cells
      3. Lists and List Functions
      4. The CONS Function
      5. The LIST Function
      6. The FIRST and REST Functions
      7. Nested Lists
    5. 3.5 Structures in Racket
      1. Structure Basics
      2. Nesting Structures
      3. Structure Transparency
    6. <code xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="literal">Interrupt</code>&#8212;Chapter Checkpoint—Chapter Checkpoint
  8. 4. (Conditions and Decisions)
    1. 4.1 How to Ask
    2. 4.2 The Conditionals: IF and Beyond
      1. One Thing at a Time with IF
      2. The Special Form that Does It All: COND
      3. A First Taste of Recursion
    3. 4.3 Cool Tricks with Conditionals
      1. Using the Stealth Conditionals AND and OR
      2. Using Functions that Return More than Just the Truth
    4. 4.4 Equality Predicates, Once More
    5. 4.5 Comparing and Testing
      1. Writing a Test
      2. What Is Not a Test
      3. Testing in the Real World
      4. More Testing Facilities
    6. <code xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="literal">Call-with-current-continuation</code>&#8212;Chapter Checkpoint—Chapter Checkpoint
  9. 4½. (define define ’define)
    1. 4½.1 Module-Level Definitions
      1. Variable Definitions
      2. Function Definitions
    2. 4½.2 Local Definitions
    3. <code xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="literal">Abort</code>&#8212;Chapter Checkpoint—Chapter Checkpoint
  10. 5. (big-bang)
    1. 5.1 Graphical User Interface
    2. 5.2 Landing a UFO
    3. 5.3 Using big-bang: Syntax and Semantics
    4. 5.4 Guessing Gooey
      1. The Data
      2. The Main Function
      3. Key-Events
      4. Rendering
      5. Time to Stop
    5. <code xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="literal">Exit</code>&#8212;Chapter Checkpoint—Chapter Checkpoint
    6. Chapter Challenges
  11. 6. (Recursion Is Easy)
    1. 6.1 Robot Snake
    2. 6.2 A Data Representation for the Snake Game
    3. 6.3 The Main Function
    4. 6.4 Clock Ticks
      1. Eating and Growing
      2. Slithering
      3. Rotting Goo
    5. 6.5 Key-Events
    6. 6.6 Rendering
    7. 6.7 End Game
    8. 6.8 Auxiliary Functions
    9. <code xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="literal">Return</code>&#8212;Chapter Checkpoint—Chapter Checkpoint
    10. Chapter Challenges
  12. 7. (Land of Lambda)
    1. 7.1 Functions as Values
    2. 7.2 Lambda
    3. 7.3 Higher-Order Fun
    4. 7.4 Two More Higher-Order Functions
    5. 7.5 Derive This!
    6. 7.6 apply
    7. <code xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="literal">Break</code>&#8212;Chapter Checkpoint—Chapter Checkpoint
  13. 8. (Mutant Structs)
    1. 8.1 Chad’s First Battle
    2. 8.2 Orc Battle
    3. 8.3 Setting Up the World, a First Step
    4. 8.4 Action: How Structs Really Work
    5. 8.5 More Actions, Setting Up the World for Good
    6. 8.6 Ready, Set, big-bang
    7. 8.7 Initializing the Orc World
    8. 8.8 Rendering the Orc World
    9. 8.9 The End of the World
    10. 8.10 Actions, A Final Look
    11. <code xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="literal">Throw</code>&#8212;Chapter Checkpoint—Chapter Checkpoint
    12. Chapter Challenges
  14. 9. (The Values of Loops)
    1. 9.1 FOR Loops
    2. 9.2 Multiple Values
    3. 9.3 Back to FOR/FOLD
    4. 9.4 More on Loops
    5. <code xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="literal">Waitpid</code>&#8212;Chapter Checkpoint—Chapter Checkpoint
  15. 10. (Dice of Doom)
    1. 10.1 The Game Tree
    2. 10.2 Dice of Doom, The Game
    3. 10.3 Designing Dice of Doom: Take One
      1. Filling in the Blanks
      2. Simplifying the Rules
      3. End of Game
      4. Controlling the Game
    4. 10.4 How Game Trees Work
    5. 10.5 Game States and Game Trees for Dice of Doom
    6. 10.6 Roll the Dice
    7. 10.7 Rendering the Dice World
    8. 10.8 Input Handling
    9. 10.9 Creating a Game Tree
      1. The Game Tree
      2. Neighbors
      3. Attacks
    10. 10.10 The End Game
    11. <code xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="literal">Kill</code>&#8212;Chapter Checkpoint—Chapter Checkpoint
    12. Chapter Challenges
  16. 11. (Power to the Lazy)
    1. 11.1 Doomsday
    2. 11.2 Lazy Evaluation
    3. 11.3 Memoized Computations
    4. 11.4 Racket Can Be Lazy
    5. <code xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="literal">Delay</code>&#8212;Chapter Checkpoint—Chapter Checkpoint
  17. 12. (Artificial Intelligence)
    1. 12.1 An Intelligent Life-form
    2. 12.2 Lazy Games
    3. 12.3 Adding Artificial Intelligence
    4. <code xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="literal">Stop-when</code>&#8212;Chapter Checkpoint—Chapter Checkpoint
    5. Chapter Challenges
  18. 13. (The World Is Not Enough)
    1. 13.1 What Is a Distributed Game?
    2. 13.2 The Data
      1. Messages
      2. Previously Fabricated Structures
      3. Packages
      4. Bundles
      5. Mail
      6. iworld Structures
    3. 13.3 The Network Postal Service
    4. 13.4 Organizing Your Universe
    5. 13.5 Distributed Guess
      1. The State of the Client and the State of the Server
      2. The Server
      3. The Client
      4. Running the Game
    6. <code xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="literal">Error</code>&#8212;Chapter Checkpoint—Chapter Checkpoint
    7. Chapter Challenges
  19. 14. (Hungry Henry)
    1. 14.1 King Henry the Hungry
    2. 14.2 Hungry Henry, the Game
    3. 14.3 Two United States
    4. 14.4 Henry’s Universe
      1. Message Data and Structures
      2. Complex Numbers Are Good Positions
      3. A Day in the Life of a Server
      4. A Day in the Life of a Client
    5. 14.5 State of the Union
      1. State of Henry
      2. State of the House
    6. 14.6 Main, Take Client
      1. The Appetizer State
      2. The Entree State
    7. 14.7 Main, Take Server
      1. The Join State and Network Events
      2. The Join State and Tick Events
      3. The Play State and Network Events
      4. The Play State and Tick Events
    8. 14.8 See Henry Run
    9. <code xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:pls="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns:ssml="http://www.w3.org/2001/10/synthesis" xmlns:svg="http://www.w3.org/2000/svg" class="literal">On-disconnect</code>&#8212;Chapter Checkpoint—Chapter Checkpoint
    10. Chapter Challenges
  20. Conclusion. Good-Bye (Close Paren)
    1. ).1 Run Racket Run
    2. ).2 Racket Is a Programming Language
    3. ).3 Racket Is a Metaprogramming Language
    4. ).4 Racket Is a Programming-Language Programming Language
    5. So Long
  21. Index
  22. About the Authors
  23. Copyright
O'Reilly logo

Chapter 6. (Recursion Is Easy)

#|

You’ve seen lists, you’ve seen very basic recursion, and you know how to use big-bang to create a simple game. You are probably wondering when we get to make an interesting game. The answer is right now. In this chapter, we will use lists and leverage the full power of recursion to make the classic Snake game.

|#

6.1 Robot Snake

Walking along a dark corridor, Chad suddenly finds himself in a deep pit among DrRacket’s mischievous minions. The crazy critters are out of control and have scattered radioactive goo all over the place. He sees a tunnel on the other side of the pit and considers jumping over the dangerous, glowing puddles—but there are too many! Chad then stumbles across a jittering, broken robotic snake. ...

The best content for your career. Discover unlimited learning on demand for around $1/day.