Transcription of Think Perl 6 - Green Tea Press
1 Think perl 6 How to Think Like a Computer Scientist1st Edition, Version 2017 Think perl 6 How to Think Like a Computer Scientist1st Edition, Version 2017 Laurent Rosenfeld, with Allen B. DowneyGreen Tea PressNeedham, MassachusettsCopyright 2017 Allen Downey, Laurent Tea Press9 Washburn AveNeedham MA 02492 Permission is granted to copy, distribute, and/or modify this document under the terms of theCreative Commons Attribution-NonCommercial Unported License, which is available original form of this book is LATEX source code. Compiling this LATEX source has the effect of gen-erating a device-independent representation of a textbook, which can be converted to other formatsand LATEX source for this book is available to the art of computer programming and to the new perl 6 language.
2 This willprobably be the first published books using perl 6 (or one of the first), a powerful, expres-sive, malleable and highly extensible programming language. But this book is less aboutPerl 6, and more about learning how to write programs for book is intended for beginners and does not require any prior programming knowl-edge, but it is my hope that even those of you with programming experience will benefitfrom reading Aim of this BookThis aim of this book is not primarily to teach perl 6, but instead to teach the art of program-ming, using the perl 6 language. After having completed this book, you should hopefullybe able to write programs to solve relatively difficult problems in perl 6, but my main aim isto teach computer science, software programming, and problem solving rather than solelyto teach the perl 6 language means that I will not cover every aspect of perl 6, but only a (relatively large, butyet incomplete) subset of it.
3 By no means is this book intended to be a reference on 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 cov-ered 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 are more than one thousand code examples in this book; study them, make sure tounderstand them, and run them.
4 When possible, try to change them and see what re likely to learn a lot from this 0. PrefaceThe History of this BookIn the course of the last three to four years, I have translated or adapted to French a numberof tutorials and articles on perl 6, and I ve also written a few entirely new ones in , these documents represented by the end of 2015 somewhere between 250 and300 pages of material on perl 6. By that time, I had probably made public more material onPerl 6 in French than all other authors taken 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.
5 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 , something happened that changed my December 2015, friends of mine were contemplating translating into French Allen sThink Python, Second Edition2. I had read an earlier edition of that book and fullysupported the idea of translating it3. As it turned out, I ended up being a co-translator andthe technical editor of the French translation of that 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 ofThink Python. Since I was in contact with Allen in the context of the Frenchtranslation, I suggested this to Allen, who warmly welcomed the idea.
6 This is how I startedto write this book late January 2016, just after having completed the work on the Frenchtranslation of his Python book is thus largely derived on Allen sThink Python, but adapted to perl 6. As ithappened, it is also much more than just a perl 6 translation of Allen s book: with quitea lot of new material, it has become a brand new book, largely indebted to Allen s book,but yet a new book for which I take all responsibility. Any errors are mine, not Allen hope is that this will be useful to the perl 6 community, and more broadly to the opensource and general computer programming communities. In an interview withLinuxVoice(July 2015), Larry Wall, the creator of perl 6, said: We do Think that perl 6 will be learnableas a first language. Hopefully this book will contribute to making this 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 perl 6 more recently.
7 Be blessed for eternity,Larry, for all of 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,1 See for # python- 2e/.3I know, it s about Python, not perl . But I don t believe in engaging in language wars and Think that we allhave to learn from other languages; to me, perl s motto, there is more than one way to do it, also means thatdoing it in Python (or some other language) is truly an acceptable ://allen- python/.viiNick, Tim, Andy, Chip, Matt, Michael, Tatsuhiko, Dave, Rafael, Chris, Stevan, Saraty, Mal-colm, Graham, Leon, Ricardo, Gurusamy, Scott and too many others to my thanks also to those who believed in this perl 6 project and made it happen, includ-ing those who quit at one point or another but contributed for some time; I know that thiswasn t always thanks to Allen Downey, who very kindly supported my idea of adapting his bookto perl 6 and helped me in many respects, but also refrained from interfering in what I wasputting into this new very warmly thank the people at O Reilly who accepted the idea of this book and sug-gested many corrections or improvements.
8 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 a lot in advance to readers who will offer comments or submit suggestions or cor-rections, as well as you see anything that needs to be corrected or that could be improved, please kindlysend your comments (at) ListI would like to thank especially Moritz Lenz and Elizabeth Mattijsen, who reviewed in de-tail 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.
9 Thanks also to Timo Paulssenand ryanschoppe who also reviewed early drafts and provided some useful thanks also to Uri Guttman, who reviewed this book and suggested a number ofsmall corrections and improvements shortly before publication. Kamimura submitted acouple of corrections in the errata list on the O Reilly web 0. PrefaceContentsPrefacevI Starting with the Basics11 The Way of the is a Program? .. perl 6 .. First Program .. Operators .. and Types .. and Natural Languages ..132 Variables, Expressions and Statements .. Names .. and Statements .. Mode .. Mode .. of Operations .. Operations ..26xContents3 Calls .. and Methods .. functions .. New Functions ( Subroutines) .. and Uses .. of Execution .. and Arguments .. and Parameters Are Local.
10 Diagrams .. Functions and Void Functions .. Signatures .. and Mutable Parameters .. and Subroutines as First-Class Citizens .. Functions and Subroutines? ..464 Loops, Conditionals, and Division and Modulo .. Expressions .. Operators .. Execution .. Execution .. Conditionals .. Conditionals .. Conditionals as Statement Modifiers .. Conditional Statement .. Loops .. Diagrams for Recursive Subroutines .. Recursion .. Input .. Arguments and the MAIN Subroutine ..655 Fruitful Values .. Development .. Functions .. Complete Programming Language .. Recursion .. of Faith .. More Example .. Types .. Subroutines ..836 Versus Equality .. Variables .. Variables and Variable Scoping .. Flow Statements (last,next, etc.).