Think Raku - GitHub

Transcription

Think RakuHow to Think Like a Computer Scientist2nd Edition, Version 0.6January 2020

Think RakuHow to Think Like a Computer Scientist2nd Edition, Version 0.6January 2020Laurent Rosenfeld, with Allen B. DowneyGreen Tea PressNeedham, Massachusetts

Copyright 2017-2020 Allen Downey, Laurent Rosenfeld.Green Tea Press9 Washburn AveNeedham MA 02492Permission is granted to copy, distribute, and/or modify this document under the terms of theCreative Commons Attribution-NonCommercial 3.0 Unported License, which is available at http://creativecommons.org/licenses/by-nc/3.0/.The original form of this book is LATEX source code. Compiling this LATEX source has the effect of generating a device-independent representation of a textbook, which can be converted to other formatsand printed.The LATEX source for this book is available from https://github.com/LaurentRosenfeld/thinkraku/

PrefaceWelcome to the art of computer programming and to the new Raku language. This isone of the first books using Raku, a powerful, expressive, malleable and highly extensibleprogramming language. But this book is less about Raku, and more about learning how towrite programs for computers.This book is intended for beginners and does not require any prior programming knowledge, but it is my hope that even those of you with programming experience will benefitfrom reading it.The Aim of this BookThis aim of this book is not primarily to teach Raku, but instead to teach the art of programming, using the Raku language. After having completed this book, you should hopefullybe able to write programs to solve relatively difficult problems in Raku, but my main aim isto teach computer science, software programming, and problem solving rather than solelyto teach the Raku language itself.This means that I will not cover every aspect of Raku, but only a (relatively large, butyet incomplete) subset of it. By no means is this book intended to be a reference on thelanguage.It is not possible to learn programming or to learn a new programming language by justreading a book; practicing is essential. This book contains a lot of exercises. You arestrongly encouraged to make a real effort to do them. And, whether successful or notin solving the exercises, you should take a look at the solutions in the Appendix, as, veryoften, several solutions are suggested with further discussion on the subject and the issuesinvolved. Sometimes, the solution section of the Appendix also introduces examples oftopics that will be covered in the next chapter–and sometimes even things that are not covered elsewhere in the book. So, to get the most out the book, I suggest you try to solve theexercises as well as review the solutions and attempt them.There are more than one thousand code examples in this book; study them, make sure tounderstand them, and run them. When possible, try to change them and see what happens.You’re likely to learn a lot from this process.The History of this BookThere is a simple thing that you need to know in order to understand the coming paragraphs. The Raku programming language has not always been called Raku. The project

viChapter 0. Prefacestarted under the Perl 6 name, because it was originally thought to be the next version ofPerl, a successor to Perl 5. With time, however, the language has grown to be syntacticallyquite different from Perl, although keeping the spirit of Perl and carrying forward the ideals of the Perl Community. Because of these differences, it was decided in October 2019 torename the Perl 6 language into Raku. The book that you are reading was originally calledThink Perl 6. This new edition thus reflects the re-branding of the language.In the course of the three to four years before the beginning of 2016, I had translated oradapted to French a number of tutorials and articles on what was still called at the timePerl 6, and I’d also written a few entirely new ones in French. 1 Together, these documentsrepresented by the end of 2015 somewhere between 250 and 300 pages of material on Perl 6.By that time, I had probably made public more material on this new language in Frenchthan all other authors taken together.In late 2015, I began to feel that a Perl 6 document for beginners was something missingthat I was willing to undertake. I looked around and found that it did not seem to existin English either. I came to the idea that, after all, it might be more useful to write such adocument initially in English, to give it a broader audience. I started contemplating writinga beginner introduction to Perl 6 programming. My idea at the time was something like a50- to 70-page tutorial and I started to gather material and ideas in this direction.Then, something happened that changed my plans.In December 2015, friends of mine were contemplating translating into French Allen B.Downey’s Think Python, Second Edition2 . I had read an earlier edition of that excellent bookand fully supported the idea of translating it3 . As it turned out, I ended up being a cotranslator and the technical editor of the French translation of that book4 .While working on the French translation of Allen’s Python book, the idea came to methat, rather than writing a tutorial on Perl 6, it might be more useful to make a “Perl 6translation” of Think Python. Since I was in contact with Allen in the context of the Frenchtranslation, I suggested this to Allen, who warmly welcomed the idea. This is how I startedto write this book late January 2016, just after having completed the work on the Frenchtranslation of his Python book.I had to use the “Perl 6” name in the preceding paragraphs, since it is the way it was namedat that point in history. From now on, I’ll use the new name of the language, Raku, unlessreferring to events prior to the renaming.This book is thus largely derived on Allen’s Think Python, but adapted to Raku. As ithappened, it is also much more than just a “Raku translation” of Allen’s book: with quite alot of new material, it has become a brand new book, largely indebted to Allen’s book, butyet a new book for which I take all responsibility. Any errors are mine, not Allen’s.My hope is that this will be useful to the Raku community, and more broadly to the opensource and general computer programming communities. In an interview with LinuxVoice(July 2015), Larry Wall, the creator of Raku, said: “We do think that Perl 6 will be learnableas a first language.” Hopefully this book will contribute to making this happen.1 See2 Seefor example ttp://greenteapress.com/wp/think-python-2e/.3 I know, it’s about Python, not Perl or Raku. But I don’t believe in engaging in “language wars” and think thatwe all have to learn from other languages; to me, Perl’s motto, “there is more than one way to do it,” also meansthat doing it in Python (or some other language) is truly an acceptable possibility.4 See ensez-python/.

viiAcknowledgmentsI just don’t know how I could thank Larry Wall to the level of gratitude that he deservesfor having created Perl in the first place, and Raku more recently. Be blessed for eternity,Larry, for all of that.And thank to you all of you who took part to this adventure (in no particular order), Tom,Damian, chromatic, Nathan, brian, Jan, Jarkko, John, Johan, Randall, Mark Jason, Ovid,Nick, Tim, Andy, Chip, Matt, Michael, Tatsuhiko, Dave, Rafael, Chris, Stevan, Saraty, Malcolm, Graham, Leon, Ricardo, Gurusamy, Scott and too many others to name.All my thanks also to those who believed in this Perl 6 and then Raku project and made ithappen, including those who quit at one point or another but contributed for some time; Iknow that this wasn’t always easy.Many thanks to Allen Downey, who very kindly supported my idea of adapting his bookto this language and helped me in many respects, but also refrained from interfering inwhat I was putting into this new book.I very warmly thank the people at O’Reilly who accepted the idea of this book and suggested many corrections or improvements. I want to thank especially Dawn Schanafelt, myeditor at O’Reilly, whose advice has truly contributed to making this a better book. Manythanks also to Charles Roumeliotis, the copy editor, and Kristen Brown, the productioneditor, who fixed many typographical problems and spelling mistakes.Thanks a lot to readers who have offered comments or submitted suggestions or corrections, as well as encouragements.If you see anything that needs to be corrected or that could be improved, please kindlysend your comments to think.perl6(at)gmail.com.Contributor ListI would like to thank especially Moritz Lenz and Elizabeth Mattijsen, who reviewed in detail drafts of this book and suggested quite a number of improvements and corrections. Lizspent a lot of time on a detailed review of the full content of this book and I am especiallygrateful to her for her numerous and very useful comments. Thanks also to Timo Paulssenand ryanschoppe who also reviewed early drafts and provided some useful suggestions.Many thanks also to Uri Guttman, who reviewed this book and suggested a number ofsmall corrections and improvements shortly before publication.Kamimura, James Lenz, and Jeff McClelland each submitted a couple of corrections in theerrata list on the O’Reilly web site. zengargoyle pointed out a spurious character in a regexand fixed it in the GitHub repository of the book. zengargoyle also suggested a clarificationin the chapter about functional programming. Another James (second name unknown tome) submitted an erratum on the O’Reilly web site. Mikhail Korenev suggested accuratecorrections to three code samples. Sébastien Dorey, Jean Forget, and Roland Schmitz sentsome e-mails suggesting a few useful corrections or improvements. Luis F. Uceta translated this book into Spanish and found in the process quite a few typos he corrected on theGithub repository. Gil Magno, zoffixznet and Joaquin Ferrero also suggested some corrections on Github. Threadless-screw fixed a couple of faulty cross-references in the chapterabout hashes. Boyd Duffee spotted two code samples that used to work fine when I wrotethem four years ago, but no longer work, due to some implementation changes. leszekdubiel suggested an improvement that probably clarifies a bit the meaning of a sentence.

viiiChapter 0. Preface

ContentsPrefacevIStarting with the Basics11The Way of the Program51.1What is a Program? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .51.2Running Raku . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .61.3The First Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .71.4Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .81.5Values and Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .81.6Formal and Natural Languages . . . . . . . . . . . . . . . . . . . . . . . . .111.7Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .121.8Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .121.9Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .132Variables, Expressions and Statements152.1Assignment Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .152.2Variable Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .162.3Expressions and Statements . . . . . . . . . . . . . . . . . . . . . . . . . . .182.4Script Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .202.5One-Liner Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .212.6Order of Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .222.7String Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .232.8Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .242.9Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .242.10Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .252.11Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .26

x34ContentsFunctions273.1Function Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .273.2Functions and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303.3Math functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .303.4Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .323.5Adding New Functions (a.k.a. Subroutines) . . . . . . . . . . . . . . . . . .323.6Definitions and Uses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .343.7Flow of Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .353.8Parameters and Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . .353.9Variables and Parameters Are Local . . . . . . . . . . . . . . . . . . . . . .373.10Stack Diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .373.11Fruitful Functions and Void Functions . . . . . . . . . . . . . . . . . . . . .383.12Function Signatures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .403.13Immutable and Mutable Parameters . . . . . . . . . . . . . . . . . . . . . .413.14Functions and Subroutines as First-Class Citizens . . . . . . . . . . . . . .423.15Why Functions and Subroutines? . . . . . . . . . . . . . . . . . . . . . . . .443.16Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .443.17Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .453.18Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .46Loops, Conditionals, and Recursion494.1Integer Division and Modulo . . . . . . . . . . . . . . . . . . . . . . . . . .494.2Boolean Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .504.3Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .524.4Conditional Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .544.5Alternative Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .544.6Chained Conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .554.7Nested Conditionals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .554.8If Conditionals as Statement Modifiers . . . . . . . . . . . . . . . . . . . . .564.9Unless Conditional Statement . . . . . . . . . . . . . . . . . . . . . . . . . .574.10For Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .574.11Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .59

Contents56xi4.12Stack Diagrams for Recursive Subroutines . . . . . . . . . . . . . . . . . . .614.13Infinite Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .614.14Keyboard Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .624.15Program Arguments and the MAIN Subroutine . . . . . . . . . . . . . . . .624.16Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .634.17Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .644.18Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .65Fruitful Subroutines695.1Return Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .695.2Incremental Development . . . . . . . . . . . . . . . . . . . . . . . . . . . .715.3Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .735.4Boolean Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .735.5A Complete Programming Language . . . . . . . . . . . . . . . . . . . . . .755.6More Recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .755.7Leap of Faith . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .775.8One More Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .785.9Checking Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .795.10Multi Subroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .805.11Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .815.12Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .835.13Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .83Iteration856.1Assignment Versus Equality . . . . . . . . . . . . . . . . . . . . . . . . . . .856.2Reassignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .866.3Updating Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .866.4The while Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .876.5Local Variables and Variable Scoping . . . . . . . . . . . . . . . . . . . . . .896.6Control Flow Statements (last, next, etc.) . . . . . . . . . . . . . . . . . . .916.7Square Roots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .936.8Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .956.9Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .956.10Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .966.11Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .96

xii7ContentsStrings997.1A String is a Sequence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .997.2Common String Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . .1007.2.1String Length . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1007.2.2Searching For a Substring Within the String . . . . . . . . . . . . . . . 1007.2.3Extracting a Substring from a String . . . . . . . . . . . . . . . . . . . 1017.2.4A Few Other Useful String Functions or Methods . . . . . . . . . . . 1027.3String Traversal With a while or for Loop . . . . . . . . . . . . . . . . . . . 1047.4Looping and Counting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7.5Regular Expressions (Regexes) . . . . . . . . . . . . . . . . . . . . . . . . . 1067.6Using Regexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1077.7Building your Regex Patterns . . . . . . . . . . . . . . . . . . . . . . . . . .1097.87.91067.7.1Literal Matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1097.7.2Wildcards and Character Classes . . . . . . . . . . . . . . . . . . . . .1097.7.3Quantifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1107.7.4Anchors and Assertions . . . . . . . . . . . . . . . . . . . . . . . . . .1117.7.5Alternation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1137.7.6Grouping and Capturing . . . . . . . . . . . . . . . . . . . . . . . . .1147.7.7Adverbs (a.k.a. Modifiers) . . . . . . . . . . . . . . . . . . . . . . . . .1147.7.8Exercises on Regexes . . . . . . . . . . . . . . . . . . . . . . . . . . . .115Putting It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1167.8.1Extracting Dates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1167.8.2Extracting an IP Address . . . . . . . . . . . . . . . . . . . . . . . . .117Substitutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1187.9.1The subst Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1197.9.2The s/search/replace/ Construct . . . . . . . . . . . . . . . . . . . . 1197.9.3Using Captures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1207.9.4Adverbs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1207.10Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1207.11Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1227.12Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .123

Contentsxiii8Case Study: Word Play1278.1Reading from and Writing to Files . . . . . . . . . . . . . . . . . . . . . . .1278.2Reading Word Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1298.3Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1308.4Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13098.4.1Words Longer Than 20 Characters (Solution) . . . . . . . . . . . . . . 1308.4.2Words with No “e” (Solution) . . . . . . . . . . . . . . . . . . . . . . .8.4.3Avoiding Other Letters (Solution) . . . . . . . . . . . . . . . . . . . . 1328.4.4Using Only Some Letters (Solution) . . . . . . . . . . . . . . . . . . . 1338.4.5Using All Letters of a List (Solution) . . . . . . . . . . . . . . . . . . . 1348.4.6Alphabetic Order (Solution) . . . . . . . . . . . . . . . . . . . . . . . .8.4.7Another Example of Reduction to a Previously Solved Problem . . . 1351311348.5Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1368.6Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1368.7Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .136Arrays and Lists1399.1Lists and Arrays Are Sequences . . . . . . . . . . . . . . . . . . . . . . . . . 1399.2Arrays Are Mutable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9.3Adding New Elements to an Array or Removing Some . . . . . . . . . . . 1439.4Stacks and Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1459.5Other Ways to Modify an Array . . . . . . . . . . . . . . . . . . . . . . . . .1469.6Traversing a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1489.7New Looping Constructs . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1499.8Map, Filter and Reduce . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1511419.8.1Reducing a List to a Value . . . . . . . . . . . . . . . . . . . . . . . . .1519.8.2The Reduction Metaoperator . . . . . . . . . . . . . . . . . . . . . . .1529.8.3Mapping a List to Another List . . . . . . . . . . . . . . . . . . . . . .1529.8.4Filtering the Elements of a List . . . . . . . . . . . . . . . . . . . . . . 1539.8.5Higher Order Functions and Functional Programming . . . . . . . . 1549.9Fixed-Size, Typed and Shaped Arrays . . . . . . . . . . . . . . . . . . . . .1559.10Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . .156

xivContents9.11Sorting Arrays or Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1579.12More Advanced Sorting Techniques . . . . . . . . . . . . . . . . . . . . . . 1589.13Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1619.14Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1629.15Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .16210 Hashes16510.1A Hash is a Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .16510.2Common Operations on Hashes . . . . . . . . . . . . . . . . . . . . . . . . .16810.3Hash as a Collection of Counters . . . . . . . . . . . . . . . . . . . . . . . . 16910.4Looping and Hashes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17010.5Reverse Lookup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17110.6Testing for Existence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17210.7Hash Keys Are Unique . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17310.8Hashes and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17410.9Memos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17610.10 Hashes as Dispatch Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . .17810.11 Global Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17910.12 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .18010.13 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .18010.14 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .18111 Case Study: Data Structure Selection18311.1The Ternary Conditional Operator . . . . . . . . . . . . . . . . . . . . . . .18311.2The given .11.3Multiple Conditionals with Junctions . . . . . . . . . . . . . . . . . . . . . . 18511.4Subroutine Named and Optional Parameters . . . . . . . . . . . . . . . . . 187when “Switch” Statement . . . . . . . . . . . . . . . . . . . 18411.4.1 Named Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . .18711.4.2 Optional Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . .18811.5Word Frequency Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . .18811.6Random Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .18911.7Word Histogram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .190

Contentsxv11.8Most Common Words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19211.9Optional Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19311.10 Hash Subtraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19411.11 Constructing New Operators . . . . . . . . . . . . . . . . . . . . . . . . . .19511.12 Sets, Bags and Mixes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19511.13 Random Words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19711.14 Markov Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19811.15 Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .20011.16 Building Your Own Data Structures . . . . . . . . . . . . . . . . . . . . . . 201II11.16.1 Linked Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .20211.16.2 Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .20311.16.3 Binary Heaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .20311.17 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .20711.18 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .20811.19 Exercises: Huffman Coding . . . . . . . . . . . . . . . . . . . . . . . . . . .20811.19.1 Variable-Length Codes . . . . . . . . . . . . . . . . . . . . . . . . . . .20811.19.2 The Frequency Table . . . . . . . . . . . . . . . . . . . . . . . . . . . .20911.19.3 Building the Huffman Code . . . . . . . . . . . . . . . . . . . . . . . .210Moving Forward12 Classes and Objects21321712.1Objects, Methods and Object-Oriented Programming . . . . . . . . . . . . 21712.2Programmer-Defined Types . . . . . . . . . . . . . . . . . . . . . . . . . . .21912.3Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .22012.4Creating Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .22212.5Rectangles and Object Composition . . . . . . . . . . . . . . . . . . . . . . 22412.6Instances as Return Values . . . . . . . . . . . . . . . . . . . . . . . . . . . .22612.7Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .22612.7.1 The Pixel Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .22712.7.2 The MovablePoint Class . . . . . . . . . . . . . . . . . . . . . . . . . .22912.7.3 Multiple Inheritance: Attractive, but Is It Wise? . . . . . . . . . . . . 230

xviContents12.8Roles and Composition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23112.8.1 Classes and Roles: An Example . . . . . . . . . . . . . . . . . . . . . .23112.8.2 Role Composition and Code Reuse . . . . . . . . . . . . . . . . . . . . 23312.8.3 Roles, Classes, Objects, and Types . . . . . . . . . . . . . . . . . . . . 23412.9Method Delegation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23512.10 Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23612.11 Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23812.11.1 Private Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .23912.11.2 Constructing Objects with Private Attributes . . . . . . . . . . . . . . 23912.12 Interface and Implementation . . . . . . . . . . . . . . . . . . . . . . . . . .24212.13 Object-Oriented Programming: A Tale . . . . . . . . . . . . . . . . . . . . . 24212.13.1 The Fable of the Shepherd . . . . . . . . . . . . . . . . . . . . . . . . .24312.13.2 The Moral . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .24312.14 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .24412.14.1 The Raku Debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . .24412.14.2 Getting Some Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . .24512.14.3 Stepping Through the Code . . . . . . . . . . . . . . . . . . . . . . . .24512.14.4 Stopping at the Right Place with Breakpoints . . . . . . . . . . . . . . 24612.14.5 Logging Information with Trace Points . . . . . . . . . . . . . . . . . 24612.14.6 Stepping Through a Regex Match . . . . . . . . . . . . . . . . . . . . 24612.15 Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13 Regexes and Grammars24724913.1A Brief Refresher . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .24913.2Declarative Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . .25013.3Captures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .25113.4Named Rules (a.k.a. Subrules) . . . . . . . . . . . . . . . . . . . . . . . . . .25213.5Grammars . . . .

How to Think Like a Computer Scientist 2nd Edition, Version 0.6 January 2020 Laurent Rosenfeld, with Allen B. Downey . programming language. But this book is less about Raku, and more about learning how to write programs for computers. . This book is thus largely derived on Allen's Think Python, but adapted to Raku. As it