® 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. (cid:127) 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) (cid:127) Employ the Lisp reader and the Lisp printer and modify their behavior through readtables, reader macros, and pretty print dispatch tables (cid:127) Write robust code using Common Lisp’s condition system in combination with assertions, handlers, and restarts (cid:127) 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 (cid:127) Develop and debug with steppers, inspectors, tracers, advices, and other tools (cid:127) 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 9781484211779 www.apress.com Common Lisp Recipes A Problem-Solution Approach Edmund Weitz CommonLispRecipes:AProblem-SolutionApproach EdmundWeitz 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 LibraryofCongressControlNumber:2015960959 Copyright©2016byEdmundWeitz Thisworkissubjecttocopyright.AllrightsarereservedbythePublisher,whetherthewholeorpartofthe materialisconcerned,specificallytherightsoftranslation,reprinting,reuseofillustrations,recitation, broadcasting,reproductiononmicrofilmsorinanyotherphysicalway,andtransmissionorinformationstorage andretrieval,electronicadaptation,computersoftware,orbysimilarordissimilarmethodologynowknownor hereafterdeveloped.Exemptedfromthislegalreservationarebriefexcerptsinconnectionwithreviewsor scholarlyanalysisormaterialsuppliedspecificallyforthepurposeofbeingenteredandexecutedonacomputer system,forexclusiveusebythepurchaserofthework.Duplicationofthispublicationorpartsthereofis permittedonlyundertheprovisionsoftheCopyrightLawofthePublisher’slocation,initscurrentversion,and permissionforusemustalwaysbeobtainedfromSpringer.Permissionsforusemaybeobtainedthrough RightsLinkattheCopyrightClearanceCenter.Violationsareliabletoprosecutionundertherespective CopyrightLaw. Trademarkednames,logos,andimagesmayappearinthisbook.Ratherthanuseatrademarksymbolwith everyoccurrenceofatrademarkedname,logo,orimageweusethenames,logos,andimagesonlyinan editorialfashionandtothebenefitofthetrademarkowner,withnointentionofinfringementofthetrademark. Theuseinthispublicationoftradenames,trademarks,servicemarks,andsimilarterms,eveniftheyarenot identifiedassuch,isnottobetakenasanexpressionofopinionastowhetherornottheyaresubjectto proprietaryrights. Whiletheadviceandinformationinthisbookarebelievedtobetrueandaccurateatthedateofpublication, neithertheauthorsnortheeditorsnorthepublishercanacceptanylegalresponsibilityforanyerrorsor omissionsthatmaybemade.Thepublishermakesnowarranty,expressorimplied,withrespecttothematerial containedherein. ManagingDirector:WelmoedSpahr LeadEditor:SteveAnglin TechnicalReviewer:HansHu¨bner EditorialBoard:SteveAnglin,LouiseCorrigan,JonathanGennick,RobertHutchinson,MichelleLowman, JamesMarkham,SusanMcDermott,MatthewMoodie,JeffreyPepper,DouglasPundick, BenRenow-Clarke,GwenanSpearing,SteveWeiss CoordinatingEditor:MarkPowers CopyEditor:KimberlyBurton-Weisman Compositor:SPiGlobal Indexer:SPiGlobal Artist:SPiGlobal DistributedtothebooktradeworldwidebySpringerScience+BusinessMediaNewYork,233SpringStreet, 6thFloor,NewYork,NY10013.Phone1-800-SPRINGER,fax(201)348-4505, [email protected],orvisitwww.springeronline.com.ApressMedia,LLCisaCaliforniaLLC andthesolemember(owner)isSpringerScience+BusinessMediaFinanceInc(SSBMFinanceInc).SSBM FinanceIncisaDelawarecorporation. Forinformationontranslations,[email protected],orvisitwww.apress.com. ApressandfriendsofEDbooksmaybepurchasedinbulkforacademic,corporate,orpromotionaluse.eBook versionsandlicensesarealsoavailableformosttitles.Formoreinformation,referenceourSpecialBulk Sales-eBookLicensingwebpageat www.apress.com/bulk-sales. Anysourcecodeorothersupplementarymaterialsreferencedbytheauthorinthistextisavailabletoreadersat www.apress.com/9781484211779.Fordetailedinformationabouthowtolocateyourbook’ssourcecode,goto www.apress.com/source-code/.ReaderscanalsoaccesssourcecodeatSpringerLinkintheSupplementary Materialsectionforeachchapter. Printedonacid-freepaper ForHeikeandMouna Table of Contents AbouttheAuthor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . XVII AbouttheTechnicalReviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . XIX Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . XXI WhoThisBookIsFor . . . . . . . . . . . . . . . . . . . . . . . . XXI WhoThisBookIs(Probably)NotFor . . . . . . . . . . . . . . XXII HowtoReadThisBook . . . . . . . . . . . . . . . . . . . . . . XXII What’sInandWhatNot . . . . . . . . . . . . . . . . . . . . . . XXIII TheHyperSpec . . . . . . . . . . . . . . . . . . . . . . . . . . . XXIV WhichImplementation . . . . . . . . . . . . . . . . . . . . . . XXIV SourceCode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . XXV TheIndex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . XXV TypographicalConventions . . . . . . . . . . . . . . . . . . . . XXV Acknowledgements . . . . . . . . . . . . . . . . . . . . . . . . XXVI 1. SymbolsandPackages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1-1. UnderstandingtheRoleofPackagesandtheSymbolNomenclature . 1 1-2. MakingUniqueSymbols . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1-3. MakingSymbolsInaccessible . . . . . . . . . . . . . . . . . . . . . . . . 9 HowCanWeFixThis? . . . . . . . . . . . . . . . . . . . . . . . . 11 1-4. AvoidingNameConflicts . . . . . . . . . . . . . . . . . . . . . . . . . 12 WhenNameConflictsDoNotOccur . . . . . . . . . . . . . . . . 15 1-5. UsingSymbolsAsStand-InsforArbitraryForms . . . . . . . . . . . 15 1-6. SearchingforSymbolsbyName . . . . . . . . . . . . . . . . . . . . . 17 1-7. IteratingThroughAllSymbolsofaPackage . . . . . . . . . . . . . . 19 WhatToDoIfYouDon’tLikeLOOP. . . . . . . . . . . . . . . . . 20 1-8. Understanding COMMON LISP’sCase(In)Sensitivity . . . . . . . . . 21 StyleHint: Don’tUseCamelCase! . . . . . . . . . . . . . . . . . 25 1-9. UsingSymbolsAsStringSubstitutes. . . . . . . . . . . . . . . . . . . 26 So,WhatShouldYouUse?. . . . . . . . . . . . . . . . . . . . . . 27 1-10. “Overloading”ofStandard COMMON LISPOperators . . . . . . . . . 28 2. Conses,Lists,andTrees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 2-1. UnderstandingConses . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 ListAccess . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 TestingWhetherSomethingIsaConsoraList . . . . . . . . . . 35 2-2. CreatingLists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 ConvertingVectorstoLists . . . . . . . . . . . . . . . . . . . . . 38 2-3. TransposingaMatrix. . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 V TableofContents 2-4. UsingListInterpolation . . . . . . . . . . . . . . . . . . . . . . . . . . 40 2-5. AddingObjectstotheEndofaList . . . . . . . . . . . . . . . . . . . 42 TheTailWaggingtheList . . . . . . . . . . . . . . . . . . . . . . 44 2-6. “Splicing”intoaList . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 2-7. DetectingSharedStructureinLists . . . . . . . . . . . . . . . . . . . . 49 IsolatingtheNon-SharedPart . . . . . . . . . . . . . . . . . . . . 51 2-8. WorkingwithTrees. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 MoreComplicatedTrees . . . . . . . . . . . . . . . . . . . . . . . 54 COMMON LISP’sStandardTreeFunctions . . . . . . . . . . . . . 55 2-9. WorkingwithStacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 2-10. ImplementingaQueue . . . . . . . . . . . . . . . . . . . . . . . . . . 56 2-11. DestructuringandPatternMatching . . . . . . . . . . . . . . . . . . . 58 3. StringsandCharacters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 3-1. GettingtheASCIICodeofaCharacter . . . . . . . . . . . . . . . . . 61 TheOtherWayAround . . . . . . . . . . . . . . . . . . . . . . . 63 TheLimit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 3-2. NamingCharacters. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 3-3. UsingDifferentCharacterEncodings . . . . . . . . . . . . . . . . . . 65 3-4. ComparingStringsorCharacters . . . . . . . . . . . . . . . . . . . . . 67 Internationalization . . . . . . . . . . . . . . . . . . . . . . . . . 70 3-5. EscapingCharactersinStringLiteralsandVariableInterpolation . . 71 IsItStillaLiteral?. . . . . . . . . . . . . . . . . . . . . . . . . . . 72 3-6. ControllingCase . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 WhatAboutUnicode? . . . . . . . . . . . . . . . . . . . . . . . . 74 3-7. AccessingorModifyingaSubstring . . . . . . . . . . . . . . . . . . . 75 3-8. FindingaCharacteroraSubstringWithinaString . . . . . . . . . . 77 3-9. TrimmingStrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 3-10. ProcessingaStringOneCharacterataTime . . . . . . . . . . . . . . 79 3-11. JoiningStrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 3-12. ReadingCSVData . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 4. NumbersandMath . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 4-1. UsingArbitrarilyLargeIntegers . . . . . . . . . . . . . . . . . . . . . 87 4-2. UnderstandingFixnums . . . . . . . . . . . . . . . . . . . . . . . . . . 89 4-3. PerformingModularArithmetic . . . . . . . . . . . . . . . . . . . . . 92 EfficiencyConsiderations . . . . . . . . . . . . . . . . . . . . . . 93 4-4. SwitchingBases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 4-5. PerformingExactArithmeticwithRationalNumbers . . . . . . . . . 96 VariousWaysofConvertingNumberstoIntegers . . . . . . . . 98 HowNottoUseFLOORandFriends . . . . . . . . . . . . . . . . 99 ConvertingFloating-PointNumberstoRationalsandViceVersa 99 MixingRationalsandFloats . . . . . . . . . . . . . . . . . . . . . 101 4-6. ControllingtheDefaultFloatFormat . . . . . . . . . . . . . . . . . . 102 4-7. EmployingArbitraryPrecisionFloats . . . . . . . . . . . . . . . . . . 104 4-8. WorkingwithComplexNumbers . . . . . . . . . . . . . . . . . . . . 106 VI TableofContents 4-9. ParsingNumbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 4-10. TestingWhetherTwoNumbersAreEqual . . . . . . . . . . . . . . . 111 Don’tEverUseEQwithNumbers! . . . . . . . . . . . . . . . . . 112 4-11. ComputingAnglesCorrectly . . . . . . . . . . . . . . . . . . . . . . . 113 4-12. CalculatingExactSquareRoots . . . . . . . . . . . . . . . . . . . . . . 115 5. ArraysandVectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 5-1. WorkingwithMultipleDimensions . . . . . . . . . . . . . . . . . . . 117 5-2. UnderstandingVectorsandSimpleArrays . . . . . . . . . . . . . . . 119 5-3. ObtainingtheSizeofanArray . . . . . . . . . . . . . . . . . . . . . . 120 5-4. ProvidingInitialContents . . . . . . . . . . . . . . . . . . . . . . . . . 121 AWarningAboutIdenticalObjects . . . . . . . . . . . . . . . . 122 5-5. TreatingArraysAsVectors . . . . . . . . . . . . . . . . . . . . . . . . 123 5-6. MakingtheLengthofVectorsFlexible . . . . . . . . . . . . . . . . . . 125 5-7. AdjustingArrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 5-8. UsinganArrayAsa“Window”intoAnotherArray. . . . . . . . . . 129 5-9. RestrictingtheElementTypeofanArray . . . . . . . . . . . . . . . . 131 UpgradingElementTypes . . . . . . . . . . . . . . . . . . . . . . 133 5-10. CopyinganArray . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 AWarningAboutObjectIdentity. . . . . . . . . . . . . . . . . . 135 6. HashTables,Maps,andSets . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 6-1. UnderstandingtheBasicsofHashTables . . . . . . . . . . . . . . . . 137 WhyDoesGETHASHReturnTwoValues? . . . . . . . . . . . . . . 139 HowManyEntriesDoestheHashTableHave? . . . . . . . . . 140 6-2. ProvidingDefaultValuesForHashTableLookups . . . . . . . . . . 140 6-3. RemovingHashTableEntries . . . . . . . . . . . . . . . . . . . . . . . 142 6-4. IteratingThroughaHashTable . . . . . . . . . . . . . . . . . . . . . . 143 Don’tRelyonAnyOrder! . . . . . . . . . . . . . . . . . . . . . . 146 Don’tModifyWhileYou’reIterating! . . . . . . . . . . . . . . . 147 Can’tThisBeMoreConcise,Please? . . . . . . . . . . . . . . . . 147 6-5. UnderstandingHashTableTestsandDefiningYourOwn . . . . . . 148 WhatIsSXHASHFor? . . . . . . . . . . . . . . . . . . . . . . . . . 152 6-6. ControllingHashTableGrowth . . . . . . . . . . . . . . . . . . . . . 152 6-7. GettingRidofHashTableEntriesAutomatically. . . . . . . . . . . . 155 6-8. RepresentingMapsAsAssociationLists . . . . . . . . . . . . . . . . 158 CombiningLookupandManipulation . . . . . . . . . . . . . . . 161 WhyWouldAnybodyPreferAlistsoverHashTables? . . . . . 162 6-9. RepresentingMapsAsPropertyLists . . . . . . . . . . . . . . . . . . 163 WhentoPreferPlistsoverAlists . . . . . . . . . . . . . . . . . . 165 ThePlistofaSymbol . . . . . . . . . . . . . . . . . . . . . . . . . 166 6-10. WorkingwithSets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 RepresentingSetsAsHashTables . . . . . . . . . . . . . . . . . 169 RepresentingSetsAsBitPatterns . . . . . . . . . . . . . . . . . . 169 7. SequencesandIteration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 7-1. FilteringaSequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 VII TableofContents 7-2. SearchingaSequence . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 7-3. SortingandMergingSequences . . . . . . . . . . . . . . . . . . . . . 175 7-4. MixingDifferentSequenceTypes . . . . . . . . . . . . . . . . . . . . . 177 7-5. Re-UsingaPartofaSequence . . . . . . . . . . . . . . . . . . . . . . 177 7-6. RepeatingSomeValuesCyclically . . . . . . . . . . . . . . . . . . . . 179 Alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 7-7. CountingDown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 7-8. Iteratingover“Chunks”ofaList . . . . . . . . . . . . . . . . . . . . . 184 7-9. ClosingoverIterationVariables . . . . . . . . . . . . . . . . . . . . . 186 7-10. “Extending”ShortSequencesinIterations . . . . . . . . . . . . . . . 187 7-11. BreakingoutofLOOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188 7-12. MakingSenseoftheMAP... Zoo . . . . . . . . . . . . . . . . . . . . . 191 TheSequenceVariants . . . . . . . . . . . . . . . . . . . . . . . . 194 7-13. DefiningYourOwnSequenceTypes . . . . . . . . . . . . . . . . . . . 194 7-14. IteratingwithITERATE . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 7-15. IteratingwithSERIES . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 WhattheExampleDoes . . . . . . . . . . . . . . . . . . . . . . . 201 8. TheLispReader. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 8-1. EmployingtheLispReaderforYourOwnCode . . . . . . . . . . . . 203 WhyREADIsPotentiallyDangerous . . . . . . . . . . . . . . . . 205 WhatREADDoesn’tDo . . . . . . . . . . . . . . . . . . . . . . . . 205 TheOptionalArgumentstoREAD . . . . . . . . . . . . . . . . . . 206 GoWild! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 8-2. TroubleshootingLiteralObjectNotation . . . . . . . . . . . . . . . . . 206 ThisAlsoAppliestoStrings! . . . . . . . . . . . . . . . . . . . . 208 8-3. EvaluatingFormsatReadTime . . . . . . . . . . . . . . . . . . . . . 208 WhattoLookOutFor . . . . . . . . . . . . . . . . . . . . . . . . 210 Alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210 8-4. EmbeddingLiteralArraysintoYourCode . . . . . . . . . . . . . . . 211 TheUsualWarning . . . . . . . . . . . . . . . . . . . . . . . . . . 212 8-5. UnderstandingtheDifferentWaystoRefertoaFunction . . . . . . . 213 8-6. RepeatingSomethingYouAlreadyTyped . . . . . . . . . . . . . . . . 214 TheyDon’tOnlyLookIdentical,TheyAreIdentical! . . . . . . . 216 8-7. SafelyExperimentingwithReadtables . . . . . . . . . . . . . . . . . . 216 TemporarilySwitchingtoStandardIOSyntax . . . . . . . . . . 218 8-8. ChangingtheSyntaxTypeofaCharacter . . . . . . . . . . . . . . . . 219 TheSixSyntaxTypes . . . . . . . . . . . . . . . . . . . . . . . . . 220 HowtoActuallyChangetheSyntaxType . . . . . . . . . . . . . 222 SomeThingsNeverChange . . . . . . . . . . . . . . . . . . . . . 222 8-9. CreatingYourOwnReaderMacros . . . . . . . . . . . . . . . . . . . 223 WhatReaderMacroFunctionsDo . . . . . . . . . . . . . . . . . 224 8-10. WorkingwithDispatchingMacroCharacters . . . . . . . . . . . . . . 226 8-11. PreservingWhitespace . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 VIII TableofContents 9. Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 9-1. UsingthePrintingPrimitives . . . . . . . . . . . . . . . . . . . . . . . 231 PrintingObjectsSoThatTheyCanBeReadBackinAgain . . . 235 Shortcuts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236 9-2. PrintingtoandintoStrings . . . . . . . . . . . . . . . . . . . . . . . . 237 9-3. PrintingNILAsaList . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 9-4. ExtendingFORMATControlStringsOverMoreThanOneLine . . . . 240 9-5. UsingFunctionsAsFORMATControls . . . . . . . . . . . . . . . . . . . 241 9-6. CreatingYourOwnFORMATDirectives . . . . . . . . . . . . . . . . . . 243 9-7. RecursiveProcessingofFORMATControls . . . . . . . . . . . . . . . . 245 9-8. ControllingHowYourOwnObjectsArePrinted . . . . . . . . . . . . 247 9-9. ControllingthePrettyPrinter . . . . . . . . . . . . . . . . . . . . . . . 249 9-10. PrintingLongLists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253 9-11. Pretty-PrintingCompoundObjects . . . . . . . . . . . . . . . . . . . 257 UsingthePrettyPrinterfromFORMAT . . . . . . . . . . . . . . . 260 9-12. ModifyingthePrettyPrinter . . . . . . . . . . . . . . . . . . . . . . . 262 10. Evaluation,Compilation,ControlFlow . . . . . . . . . . . . . . . . . . . . . 265 10-1. ComparingArbitraryLispObjects . . . . . . . . . . . . . . . . . . . . 265 ComparingState . . . . . . . . . . . . . . . . . . . . . . . . . . . 266 Constants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 10-2. UsingConstantVariablesasKeysinCASEMacros . . . . . . . . . . . 269 10-3. UsingArbitraryVariableNamesforKeywordParameters . . . . . . 271 KeywordNamesDon’tHavetoBeKeywords . . . . . . . . . . 272 KeywordDamesDon’tHavetoBeConstant . . . . . . . . . . . 273 10-4. Creating“StaticLocalVariables,”Likein C . . . . . . . . . . . . . . . 273 10-5. “Preponing”theComputationofValues . . . . . . . . . . . . . . . . 275 10-6. ModifyingtheBehaviorofFunctionsYouDon’tHavetheSourceOf 278 10-7. SwappingtheValuesofVariables(orPlaces) . . . . . . . . . . . . . . 280 10-8. CreatingYourOwnUpdateFormsfor“Places” . . . . . . . . . . . . 283 UsingDEFSETF . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 UsingDEFINE-SETF-EXPANDER . . . . . . . . . . . . . . . . . . . 286 So,WhichOneDoIUse? . . . . . . . . . . . . . . . . . . . . . . 291 UsingDEFINE-MODIFY-MACRO . . . . . . . . . . . . . . . . . . . . 291 Multiple-ValuedPlaces . . . . . . . . . . . . . . . . . . . . . . . 293 10-9. WorkingwithEnvironments . . . . . . . . . . . . . . . . . . . . . . . 294 10-10. CommentingOutPartsofYourCode . . . . . . . . . . . . . . . . . . 299 SomeNotesAbout;and#| . . . . . . . . . . . . . . . . . . . . . 302 How;,#|,andOthersAreImplemented . . . . . . . . . . . . . 302 11. Concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303 11-1. ManagingLispProcesses . . . . . . . . . . . . . . . . . . . . . . . . . 304 EscapeHatches . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307 ThreadsAreExpensive . . . . . . . . . . . . . . . . . . . . . . . 308 11-2. AccessingSharedResourcesConcurrently . . . . . . . . . . . . . . . 308 Locks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312 IX TableofContents AtomicOperations . . . . . . . . . . . . . . . . . . . . . . . . . . 313 MoreProblems . . . . . . . . . . . . . . . . . . . . . . . . . . . . 314 11-3. UsingSpecialVariablesinConcurrentPrograms . . . . . . . . . . . . 317 Per-ThreadInitialBindings . . . . . . . . . . . . . . . . . . . . . 319 VariablesThatAreAlwaysGlobal . . . . . . . . . . . . . . . . . 319 11-4. CommunicatingwithOtherThreads . . . . . . . . . . . . . . . . . . . 320 Alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322 11-5. ParallelizingAlgorithmsWithoutThreadsandLocks . . . . . . . . . 322 WhattheExampleDoes . . . . . . . . . . . . . . . . . . . . . . . 325 Fine-Tuning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326 Ptrees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326 Alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330 11-6. DeterminingtheNumberofCores . . . . . . . . . . . . . . . . . . . . 330 12. ErrorHandlingandAvoidance . . . . . . . . . . . . . . . . . . . . . . . . . . 333 12-1. CheckingTypesatRunTime . . . . . . . . . . . . . . . . . . . . . . . 333 Alternatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335 12-2. AddingAssertionstoYourCode . . . . . . . . . . . . . . . . . . . . . 336 DisablingAssertionsin“ProductionCode” . . . . . . . . . . . . 338 12-3. DefiningYourOwnConditions . . . . . . . . . . . . . . . . . . . . . . 338 HowConditionsArePrinted . . . . . . . . . . . . . . . . . . . . 340 12-4. SignalingaCondition . . . . . . . . . . . . . . . . . . . . . . . . . . . 341 ConditionDesignators . . . . . . . . . . . . . . . . . . . . . . . . 343 12-5. HandlingConditions. . . . . . . . . . . . . . . . . . . . . . . . . . . . 344 IgnoringErrors . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349 12-6. ProvidingandUsingRestarts . . . . . . . . . . . . . . . . . . . . . . . 350 VisibleRestarts . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354 PredefinedRestarts . . . . . . . . . . . . . . . . . . . . . . . . . . 355 12-7. GettingRidofWarningMessages . . . . . . . . . . . . . . . . . . . . 356 12-8. ProtectingCodefromNon-LocalExits . . . . . . . . . . . . . . . . . . 357 “WITH-”Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360 13. Objects,Classes,Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361 13-1. DefiningTypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361 CompoundTypeSpecifiers . . . . . . . . . . . . . . . . . . . . . 363 DerivedTypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365 13-2. UsingClassesAsTypes . . . . . . . . . . . . . . . . . . . . . . . . . . 366 13-3. WritingMethodsforBuilt-InClasses . . . . . . . . . . . . . . . . . . 367 13-4. ProvidingConstructorsforYourClasses . . . . . . . . . . . . . . . . 369 13-5. MarkingSlotsAs“Private” . . . . . . . . . . . . . . . . . . . . . . . . 372 13-6. ChangingtheArgumentPrecedenceOrder . . . . . . . . . . . . . . . 374 13-7. AutomaticallyInitializingSlotsonFirstUsage . . . . . . . . . . . . . 376 13-8. ChangingandRedefiningClassesontheFly . . . . . . . . . . . . . . 377 ObjectsChangingTheirClass . . . . . . . . . . . . . . . . . . . . 380 RedefiningClasses . . . . . . . . . . . . . . . . . . . . . . . . . . 381 13-9. MakingYourObjectsExternalizable . . . . . . . . . . . . . . . . . . . 383 X
Description: