Common Lisp Recipes
Total Page:16
File Type:pdf, Size:1020Kb
BOOKS FOR PROFESSIONALS BY PROFESSIONALS® Weitz Common Lisp Recipes Common Lisp Recipes is a collection of solutions to problems and answers to questions you are likely to encounter when writing real-world applications in Common Lisp. Written by an author who has used Common Lisp in many successful commercial projects over more than a decade, this book covers areas as diverse as web programming, databases, graphical user interfaces, integration with other programming languages, multi-threading, and mobile devices as well as debugging techniques and optimization, to name just a few. It is also the first Common Lisp book to tackle such advanced topics as environment access, logical pathnames, Gray streams, delivery of executables, pretty printing, setf expansions, or changing the syntax of Common Lisp. The book is organized around specific problems or questions each followed by ready-to-use example solutions and clear explanations of the concepts involved, plus pointers to alternatives and more information. Each recipe can be read independently of the others and thus the book will earn a special place on your bookshelf as a reference work you always want to have within reach. Common Lisp Recipes is written in a style that mixes hands-on, no-frills pragmatism with precise information and prudent mentorship. • Use Common Lisp’s object system (CLOS) and work with derived types, non-standard method combinations, class changes on the fly, or the Metaobject Protocol (MOP) • Employ the Lisp reader and the Lisp printer and modify their behavior through readtables, reader macros, and pretty print dispatch tables • Write robust code using Common Lisp’s condition system in combination with assertions, handlers, and restarts • Call other languages like C, C++, or Java from Common Lisp, embed Common Lisp into other languages, or deliver Common Lisp code as a binary or as a shared library • Develop and debug with steppers, inspectors, tracers, advices, and other tools • Optimize your code using techniques such as profilers, the disassembler, compiler hints, declarations, stack allocation, memoization, inlining, special data structures, or compiler macros ISBN 978-1-4842-1177-9 Shelve in: 56999 Programming Languages/General User level: Intermediate–Advanced SOURCE CODE ONLINE 9781484 211779 www.apress.com Common Lisp Recipes A Problem-Solution Approach Edmund Weitz Common Lisp Recipes: A Problem-Solution Approach Edmund Weitz Hamburg, Germany ISBN-13 (pbk): 978-1-4842-1177-9 ISBN-13 (electronic): 978-1-4842-1176-2 DOI: 10.1007/978-1-4842-1176-2 Library of Congress Control Number: 2015960959 Copyright © 2016 by Edmund Weitz This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher’s location, in its current version, and permission for use must always be obtained from Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law. Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they arenot identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. Managing Director: Welmoed Spahr Lead Editor: Steve Anglin Technical Reviewer: Hans H¨ubner Editorial Board: Steve Anglin, Louise Corrigan, Jonathan Gennick, Robert Hutchinson, Michelle Lowman, James Markham, Susan McDermott, Matthew Moodie, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Gwenan Spearing, Steve Weiss Coordinating Editor: Mark Powers Copy Editor: Kimberly Burton-Weisman Compositor: SPi Global Indexer: SPi Global Artist: SPi Global Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail [email protected], or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales-eBook Licensing web page at www.apress.com/bulk-sales. Any source code or other supplementary materials referenced by the author in this text is available to readers at www.apress.com/9781484211779. For detailed information about how to locate your book’s source code, go to www.apress.com/source-code/. Readers can also access source code at SpringerLink in the Supplementary Material section for each chapter. Printed on acid-free paper For Heike and Mouna Table of Contents About the Author...................................XVII About the Technical Reviewer.............................XIX Preface ..........................................XXI Who This Book Is For ........................XXI Who This Book Is (Probably) Not For . XXII How to Read This Book . XXII What’s In and What Not . XXIII The HyperSpec...........................XXIV Which Implementation . XXIV Source Code.............................XXV The Index ..............................XXV Typographical Conventions....................XXV Acknowledgements . XXVI 1. Symbols and Packages................................ 1 1-1. Understanding the Role of Packages and the Symbol Nomenclature.1 1-2. Making Unique Symbols ..........................5 1-3. Making Symbols Inaccessible........................ 9 How Can We Fix This?........................ 11 1-4. Avoiding Name Conflicts......................... 12 When Name Conflicts Do Not Occur................ 15 1-5. Using Symbols As Stand-Ins for Arbitrary Forms ........... 15 1-6. Searching for Symbols by Name..................... 17 1-7. Iterating Through All Symbols of a Package .............. 19 What To Do If You Don’t Like LOOP . 20 1-8. Understanding COMMON LISP’s Case (In)Sensitivity......... 21 Style Hint: Don’t Use CamelCase! ................. 25 1-9. Using Symbols As String Substitutes................... 26 So, What Should You Use?...................... 27 1-10. “Overloading” of Standard COMMON LISP Operators......... 28 2. Conses, Lists, and Trees .............................. 31 2-1. Understanding Conses........................... 31 List Access............................... 34 Testing Whether Something Is a Cons or a List.......... 35 2-2. Creating Lists................................ 37 Converting Vectors to Lists ..................... 38 2-3. Transposing a Matrix............................ 39 V Table of Contents 2-4. Using List Interpolation.......................... 40 2-5. Adding Objects to the End of a List ................... 42 The Tail Wagging the List...................... 44 2-6. “Splicing” into a List............................ 45 2-7. Detecting Shared Structure in Lists.................... 49 Isolating the Non-Shared Part.................... 51 2-8. Working with Trees............................. 51 More Complicated Trees....................... 54 COMMON LISP’s Standard Tree Functions............. 55 2-9. Working with Stacks............................ 55 2-10. Implementing a Queue .......................... 56 2-11. Destructuring and Pattern Matching................... 58 3. Strings and Characters............................... 61 3-1. Getting the ASCII Code of a Character ................. 61 The Other Way Around ....................... 63 The Limit ............................... 63 3-2. Naming Characters............................. 63 3-3. Using Different Character Encodings .................. 65 3-4. Comparing Strings or Characters..................... 67 Internationalization ......................... 70 3-5. Escaping Characters in String Literals and Variable Interpolation . 71 Is It Still a Literal?........................... 72 3-6. Controlling Case.............................. 72 What About Unicode?........................ 74 3-7. Accessing or Modifying