Conrad Barski, M.D. LAND OF LISP LAND OF LISP Learn to Program in Lisp, One Game at a Time! by Conrad Barski, M.D. San Francisco LAND OF LISP. Copyright © 2011 by Conrad Barski, M.D. 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 written permission of the copyright owner and the publisher. Printed in Canada 14 13 12 11 10 1 2 3 4 5 6 7 8 9 ISBN-10: 1-59327-281-2 ISBN-13: 978-1-59327-281-4 Publisher: William Pollock Production Editors: Ansel Staton and Serena Yang Developmental Editor: Keith Fancher Technical Reviewers: Philip Fominykh and Heow Eide-Goodman Copyeditor: Marilyn Smith Compositor: Susan Glinert Stevens Proofreader: Linda Seifert Indexer: Nancy Guenther For information on book distributors or translations, 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 Barski, Conrad. Land of Lisp : learn to program in Lisp, one game at a time! / by Conrad Barski. p. cm. Includes index. ISBN-13: 978-1-59327-281-4 ISBN-10: 1-59327-281-2 1. Computer games--Programming 2. COMMON LISP (Computer program language) 3. LISP (Computer program language) I. Title. QA76.76.C672B3693 2010 794.8'1526--dc22 2010026755 No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company 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. For Lauren B R I E F C O N T E N T S Acknowledgments........................................................................................................xvii Introduction....................................................................................................................1 SECTION I: LISP IS POWER Chapter 1: Getting Started with Lisp................................................................................15 Chapter 2: Creating Your First Lisp Program.....................................................................21 Chapter 3: Exploring the Syntax of Lisp Code...................................................................31 SECTION II: LISP IS SYMMETRY Chapter 4: Making Decisions with Conditions..................................................................49 Chapter 5: Building a Text Game Engine.........................................................................67 Chapter 6: Interacting with the World: Reading and Printing in Lisp....................................85 Chapter 6.5: lambda: A Function So Important It Deserves Its Own Chapter.......................103 Chapter 7: Going Beyond Basic Lists.............................................................................107 Chapter 8: This Ain’t Your Daddy’s Wumpus..................................................................129 Chapter 9: Advanced Datatypes and Generic Programming............................................153 SECTION III: LISP IS HACKING..................................................................................191 Chapter 10: Looping with the loop Command................................................................195 Chapter 11: Printing Text with the format Function...........................................................221 Chapter 12: Working with Streams...............................................................................237 Chapter 13: Let’s Create a Web Server!........................................................................253 Functional Programming Is Beautiful..............................................................................269 SECTION IV: LISP IS SCIENCE Chapter 14: Ramping Lisp Up a Notch with Functional Programming................................291 Chapter 15: Dice of Doom, a Game Written in the Functional Style..................................303 Chapter 16: The Magic of Lisp Macros..........................................................................339 Chapter 17: Domain-Specific Languages........................................................................355 Chapter 18: Lazy Programming....................................................................................375 Chapter 19: Creating a Graphical, Web-Based Version of Dice of Doom..........................401 Chapter 20: Making Dice of Doom More Fun.................................................................417 Epilogue....................................................................................................................429 Index.........................................................................................................................465 viii Brief Contents
Description: