Think Perl 6

Book description

Want to learn how to program and think like a computer scientist? This practical guide gets you started on your programming journey with the help of Perl 6, the younger sister of the popular Perl programming language. Ideal for beginners, this hands-on book includes over 100 exercises with multiple solutions, and more than 1,000 code examples so you can quickly practice what you learn. Experienced programmers—especially those who know Perl 5—will also benefit.

Divided into two parts, Think Perl 6 starts with basic concepts that every programmer needs to know, and then focuses on different programming paradigms and some more advanced programming techniques. With two semesters’ worth of lessons, this book is the perfect teaching tool for computer science beginners in colleges and universities.

  • Learn basic concepts including variables, expressions, statements, functions, conditionals, recursion, and loops
  • Understand commonly used basic data structures and the most useful algorithms
  • Dive into object-oriented programming, and learn how to construct your own types and methods to extend the language
  • Use grammars and regular expressions to analyze textual content
  • Explore how functional programming can help you make your code simpler and more expressive

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. The Aim of This Book
    2. The History of This Book
      1. Conventions Used in This Book
      2. Using Code Examples
      3. O’Reilly Safari
      4. How to Contact Us
    3. Acknowledgments
    4. Contributor List
  2. I. Starting with the Basics
  3. 1. The Way of the Program
    1. What Is a Program?
    2. Running Perl 6
    3. The First Program
    4. Arithmetic Operators
    5. Values and Types
    6. Formal and Natural Languages
    7. Debugging
    8. Glossary
    9. Exercises
  4. 2. Variables, Expressions, and Statements
    1. Assignment Statements
    2. Variable Names
    3. Expressions and Statements
    4. Script Mode
    5. One-Liner Mode
    6. Order of Operations
    7. String Operations
    8. Comments
    9. Debugging
    10. Glossary
    11. Exercises
  5. 3. Functions
    1. Function Calls
    2. Functions and Methods
    3. Math Functions
    4. Composition
    5. Adding New Functions (a.k.a. Subroutines)
    6. Definitions and Uses
    7. Flow of Execution
    8. Parameters and Arguments
    9. Variables and Parameters Are Local
    10. Stack Diagrams
    11. Fruitful Functions and Void Functions
    12. Function Signatures
    13. Immutable and Mutable Parameters
    14. Functions and Subroutines as First-Class Citizens
    15. Why Functions and Subroutines?
    16. Debugging
    17. Glossary
    18. Exercises
  6. 4. Loops, Conditionals, and Recursion
    1. Integer Division and Modulo
    2. Boolean expressions
    3. Logical Operators
    4. Conditional Execution
    5. Alternative Execution
    6. Chained Conditionals
    7. Nested Conditionals
    8. if Conditionals as Statement Modifiers
    9. Unless Conditional Statement
    10. for Loops
    11. Recursion
    12. Stack Diagrams for Recursive Subroutines
    13. Infinite Recursion
    14. Keyboard Input
    15. Program Arguments and the MAIN Subroutine
    16. Debugging
    17. Glossary
    18. Exercises
  7. 5. Fruitful Subroutines
    1. Return Values
    2. Incremental Development
    3. Composition
    4. Boolean Functions
    5. A Complete Programming Language
    6. More Recursion
    7. Leap of Faith
    8. One More Example
    9. Checking Types
    10. Multi Subroutines
    11. Debugging
    12. Glossary
    13. Exercises
  8. 6. Iteration
    1. Assignment Versus Equality
    2. Reassignment
    3. Updating Variables
    4. The while Statement
    5. Local Variables and Variable Scoping
    6. Control Flow Statements (last, next, etc.)
    7. Square Roots
    8. Algorithms
    9. Debugging
    10. Glossary
    11. Exercises
  9. 7. Strings
    1. A String Is a Sequence
    2. Common String Operators
      1. String Length
      2. Searching for a Substring Within the String
      3. Extracting a Substring from a String
      4. A Few Other Useful String Functions or Methods
    3. String Traversal with a while or for Loop
    4. Looping and Counting
    5. Regular Expressions (Regexes)
    6. Using Regexes
    7. Building your Regex Patterns
      1. Literal Matching
      2. Wildcards and Character Classes
      3. Quantifiers
      4. Anchors and Assertions
      5. Alternation
      6. Grouping and Capturing
      7. Adverbs (a.k.a. Modifiers)
      8. Exercises on Regexes
    8. Putting It All Together
      1. Extracting Dates
      2. Extracting an IP Address
    9. Substitutions
      1. The subst Method
      2. The s/search/replace/ Construct
      3. Using Captures
      4. Adverbs
    10. Debugging
    11. Glossary
    12. Exercises
  10. 8. Case Study: Word Play
    1. Reading from and Writing to Files
    2. Reading Word Lists
    3. Exercises
    4. Search
      1. Words Longer Than 20 Characters (Solution)
      2. Words with No “e” (Solution)
      3. Avoiding Other Letters (Solution)
      4. Using Only Some Letters (Solution)
      5. Using All Letters of a List (Solution)
      6. Alphabetic Order (Solution)
      7. Another Example of Reduction to a Previously Solved Problem
    5. Debugging
    6. Glossary
    7. Exercises
  11. 9. Arrays and Lists
    1. Lists and Arrays Are Sequences
    2. Arrays Are Mutable
    3. Adding New Elements to an Array or Removing Some
    4. Stacks and Queues
    5. Other Ways to Modify an Array
    6. Traversing a List
    7. New Looping Constructs
    8. Map, Filter, and Reduce
      1. Reducing a List to a Value
      2. The Reduction Metaoperator
      3. Mapping a List to Another List
      4. Filtering the Elements of a List
      5. Higher-Order Functions and Functional Programming
    9. Fixed-Size, Typed, and Shaped Arrays
    10. Multidimensional Arrays
    11. Sorting Arrays or Lists
    12. More Advanced Sorting Techniques
    13. Debugging
    14. Glossary
    15. Exercises
  12. 10. Hashes
    1. A Hash Is a Mapping
    2. Common Operations on Hashes
    3. Hash as a Collection of Counters
    4. Looping and Hashes
    5. Reverse Lookup
    6. Testing for Existence
    7. Hash Keys Are Unique
    8. Hashes and Arrays
    9. Memos
    10. Hashes as Dispatch Tables
    11. Global Variables
    12. Debugging
    13. Glossary
    14. Exercises
  13. 11. Case Study: Data Structure Selection
    1. The Ternary Conditional Operator
    2. The given ... when “Switch” Statement
    3. Subroutine Named and Optional Parameters
      1. Named Parameters
      2. Optional Parameters
    4. Word Frequency Analysis
    5. Random Numbers
    6. Word Histogram
    7. Most Common Words
    8. Optional Parameters
    9. Hash Subtraction
    10. Constructing New Operators
    11. Sets, Bags, and Mixes
    12. Random Words
    13. Markov Analysis
    14. Data Structures
    15. Building Your Own Data Structures
      1. Linked Lists
      2. Trees
      3. Binary Heaps
    16. Debugging
    17. Glossary
    18. Exercises: Huffman Coding
      1. Variable-Length Codes
      2. The Frequency Table
      3. Building the Huffman Code
  14. II. Moving Forward
  15. 12. Classes and Objects
    1. Objects, Methods, and Object-Oriented Programming
    2. Programmer-Defined Types
    3. Attributes
    4. Creating Methods
    5. Rectangles and Object Composition
    6. Instances as Return Values
    7. Inheritance
      1. The Pixel Class
      2. The MovablePoint Class
      3. Multiple Inheritance: Attractive, but Is It Wise?
    8. Roles and Composition
      1. Classes and Roles: An Example
      2. Role Composition and Code Reuse
      3. Roles, Classes, Objects, and Types
    9. Method Delegation
    10. Polymorphism
    11. Encapsulation
      1. Private Methods
      2. Constructing Objects with Private Attributes
    12. Interface and Implementation
    13. Object-Oriented Programming: A Tale
      1. The Fable of the Shepherd
      2. The Moral
    14. Debugging
      1. The Perl 6 Debugger
      2. Getting Some Help
      3. Stepping Through the Code
      4. Stopping at the Right Place with Breakpoints
      5. Logging Information with Trace Points
      6. Stepping Through a Regex Match
    15. Glossary
  16. 13. Regexes and Grammars
    1. A Brief Refresher
    2. Declarative Programming
    3. Captures
    4. Named Rules (a.k.a. Subrules)
    5. Grammars
    6. Grammar Inheritance
    7. Actions Objects
    8. A Grammar for Parsing JSON
      1. The JSON Format
      2. Our JSON Sample
      3. Writing the JSON Grammar Step by Step
      4. The JSON Grammar
      5. Adding Actions
    9. Inheritance and Mutable Grammars
    10. Debugging
    11. Glossary
    12. Exercise: A Grammar for an Arithmetic Calculator
  17. 14. Functional Programming in Perl
    1. Higher-Order Functions
      1. A Refresher on Functions as First-Class Objects
      2. Anonymous Subroutines and Lambdas
      3. Closures
    2. List Processing and Pipeline Programming
      1. Feed and Backward Feed Operators
      2. The Reduction Metaoperator
      3. The Hyperoperator
      4. The Cross (X) and Zip (Z) Operators
      5. List Operators: A Summary
      6. Creating New Operators
    3. Creating Your Own Map-Like Functions
      1. Custom versions of map, grep, etc.
      2. Our Own Version of a Sort Function
      3. An Iterator Version of map
      4. An Iterator Version of grep
    4. The gather and take Construct
    5. Lazy Lists and the Sequence Operator
      1. The Sequence Operator
      2. Infinite Lists
      3. Using an Explicit Generator
    6. Currying and the Whatever Operator
      1. Creating a Curried Subroutine
      2. Currying an Existing Subroutine with the assuming Method
      3. Currying with the Whatever Star Parameter
    7. Using a Functional Programming Style
      1. The Merge Sort Algorithm
      2. A Nonfunctional Implementation of Merge Sort
      3. A Functional Implementation of Merge Sort
    8. Debugging
    9. Glossary
    10. Exercise: Quick Sort
  18. 15. Some Final Advice
    1. Make It Clear, Keep It Simple
    2. Dos and Don’ts
    3. Use Idioms
    4. What’s Next?
  19. A. Solutions to the Exercises
    1. Exercises of Chapter 3: Functions and Subroutines
      1. Exercise 3-1: Subroutine right-justify
      2. Exercise 3-2: Subroutine do-twice
      3. Exercise 3-3: Subroutine print-grid
    2. Exercises of Chapter 4: Conditionals and Recursion
      1. Exercise Suggested in “Stack Diagrams for Recursive Subroutines”: Subroutine do-n-times
      2. Exercise 4-1: Days, Hours, Minutes, and Seconds
      3. Exercise 4-2: Fermat’s Theorem
      4. Exercise 4-3: Is It a Triangle?
      5. Exercise 4-4: The Fibonacci Numbers
      6. Exercise 4-5: The recurse Subroutine
    3. Exercises of Chapter 5: Fruitful Functions
      1. Exercise: Compare
      2. Exercise: Hypotenuse
      3. Exercise: Chained Relational Operators
      4. Exercise 5-2: The Ackermann Function
      5. Exercise 5-3: Palindromes
      6. Exercise 5-4: Powers
      7. Exercise 5-5: Finding the GCD of Two Numbers
    4. Exercises of Chapter 6: Iteration
      1. Exercise 6-1: Square Root
      2. Exercise 6-2: Pi Estimate
    5. Exercises of Chapter 7: Strings
      1. Exercise: String Traversal
      2. Exercise: The Ducklings
      3. Exercise: Counting the Letters of a String
      4. Exercise: Simulating a Regex with a Loop
      5. Exercise: Regex Exercises
      6. Exercise: is-reverse Subroutine
      7. Exercise 7-1: Counting Letters
      8. Exercise 7-2: Lowercase Letters
      9. Exercise 7-3: Caesar’s Cipher
    6. Exercises of Chapter 8: Word Play
      1. Exercise 8-7: Consecutive Double Letters
      2. Exercise 8-8: Palindromes in Odometers
      3. Exercise 8-9: Palindromes in Ages
    7. Exercises of Chapter 9: Arrays and Lists
      1. Exercise: Implementing a Queue
      2. Exercise: Other Ways to Modify an Array
      3. Exercise: Mapping and Filtering the Elements of a List
      4. Exercise: Advanced Sorting Techniques
      5. Exercise 9-1: Nested Sum
      6. Exercise 9-2: Cumulative Sum
      7. Exercise 9-3: Middle
      8. Exercise 9-4: Chop
      9. Exercise 9-5: Subroutine is-sorted
      10. Exercise 9-6: Subroutine is-anagram
      11. Exercise 9-7: Subroutine has-duplicates
      12. Exercise 9-8: Simulating the Birthday Paradox
      13. Exercise 9-9: Comparing push and unshift
      14. Exercise 9-10: Bisection Search in a List
      15. Exercise 9-11: Reverse Pairs
      16. Exercise 9-12: Interlocking Words
    8. Exercises of Chapter 10: Hashes
      1. Exercise: A Hash Is a Mapping
      2. Exercise 10-1: Storing the Word List into a Hash
      3. Exercise 10-2: Memoizing the Ackermann Function
      4. Exercise 10-3: Finding Duplicates with a Hash
      5. Exercise 10-4: Rotate Pairs
      6. Exercise 10-5: Homophones
    9. Exercises of Chapter 11
      1. Exercise: The given ... when switch statement
      2. Exercise: Constructing New Operators
      3. Exercise: Sets, Bags, and Mixes
      4. Exercise: Random Words
      5. Exercise: Markov Analysis
      6. Exercise: The Huffman Code
    10. Exercises of Chapter 13: Regexes and Grammars
      1. Exercise 13-1: Getting the February Dates Right
      2. Exercise: A Grammar for an Arithmetic Calculator
    11. Exercises of Chapter 14: Functional Programming
      1. Exercise: Making a Functional Implementation of Quick Sort
  20. Index

Product information

  • Title: Think Perl 6
  • Author(s): Laurent Rosenfeld, Allen B. Downey
  • Release date: May 2017
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781491980507