Table Of ContentRealm of Racket
Realm of
Racket
learn to Program,
one Game at a time!
(list
Forrest Bice
Rose DeMaio
Spencer Florence
Feng-Yun Mimi Lin
Scott Lindeman
Nicole Nussbaum
Eric Peterson
Ryan Plessner
David Van Horn
Matthias Felleisen
Conrad Barski, MD)
San Francisco
Realm of Racket. Copyright © 2013 by Matthias Felleisen, David Van Horn, Conrad Barski, MD, Forrest Bice,
Rose DeMaio, Spencer Florence, Feng-Yun Mimi Lin, Scott Lindeman, Nicole Nussbaum, Eric Peterson, Ryan Plessner.
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or
mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior writ-
ten permission of the copyright owner and the publisher.
Printed in USA
First printing
17 16 15 14 13 1 2 3 4 5 6 7 8 9
ISBN-10: 1-59327-491-2
ISBN-13: 978-1-59327-491-7
Publisher: William Pollock
Production Editor: Alison Law
Cover Illustration: Eric Peterson
Illustrations: Feng-Yun Mimi Lin and Eric Peterson
Interior Design and Composition: Rose DeMaio and Nicole Nussbaum
Developmental Editors: Keith Fancher and William Pollock
Copyeditor: Marilyn Smith
Proofreader: Paula L. Fleming
For information on distribution, translations, or bulk sales, please contact No Starch Press, Inc. directly:
No Starch Press, Inc.
38 Ringold Street, San Francisco, CA 94103
phone: 415.863.9900; fax: 415.863.9950; [email protected]; www.nostarch.com
Library of Congress Cataloging-in-Publication Data:
Felleisen, Matthias.
Realm of Racket : learn to program, one game at a time! / by Matthias Felleisen, Conrad
Barski, Forrest Bice, Rose DeMaio, Spencer Florence, Feng-yun Mimi Lin, Scott Lindeman,
Nicole Nussbaum, Eric Peterson, Ryan Plessner, and David Van Horn.
pages cm
Includes index.
ISBN-13: 978-1-59327-491-7
ISBN-10: 1-59327-491-2
1. Racket (Computer program language) 2. LISP (Computer program language) 3. Computer
programming. I. Title.
QA76.73.R33F45 2013
005.1--dc23
2013002308
No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and com-
pany names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol
with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of
the trademark owner, with no intention of infringement of the trademark.
The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been taken
in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity
with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it.
to the memory of John mccarthy
September 4, 1927–october 24, 2011
and to
my parents, Sarah and Steve
my beloved Noni, and my insanely fantastic Mom and Dad
my father, Brian
my beloved family, especially my best friend and ger ger, Dennis
my parents, Jeff and Amy
M and D, Magoo and Sunny
my parents, Joy and Dan
Richard, Lynda, and Morgan
Barbara and Pete
Christopher and Rachel
Lauren and Ava
BRief contentS
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
Preface: Hello World . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Introduction: Open Paren . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Chapter 1: Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Chapter 2: A First Racket Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Chapter 3: Basics of Racket . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Chapter 4: Conditions and Decisions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Chapter 4½: define define 'define . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Chapter 5: big-bang . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Chapter 6: Recursion Is Easy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Chapter 7: Land of Lambda . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Chapter 8: Mutant Structs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Chapter 9: The Values of Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Chapter 10: Dice of Doom . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Chapter 11: Power to the Lazy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Chapter 12: Artificial Intelligence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
Chapter 13: The World Is Not Enough . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Chapter 14: Hungry Henry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231
Good-Bye: Close Paren . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265