An accessible yet rigorous crash course on recursive programming using Python and JavaScript examples.
Recursion, and recursive algorithms, have a reputation for being intimidating. They're seen as an advanced computer science topic often brought up in coding interviews. Moreover, coders often perceive the use of a recursive algorithm as a sophisticated solution that only true programmers can produce. But there's nothing magical about recursion. Its fearsome reputation is more a product of poor teaching than of the complexity of recursion itself.
This book teaches the basics of recursion, exposes the ways it's often poorly taught, and clarifies the fundamental principles behind all recursive algorithms. It is project-based, containing complete, runnable programs in both Python and JavaScript, and covers several common recursive algorithms for tasks like calculating factorials, producing numbers in the Fibonacci sequence, tree traversal, maze solving, binary search, quicksort and merge sort, Karatsuba multiplication, permutations and combinations, and solving the eight queens problem.
The book also explains tail call optimization and memoization, concepts often employed to produce effective recursive algorithms, and the call stack, which is a critical part of how recursive functions work but is almost never explicitly pointed out in lessons on recursion. The last chapter, on fractals, culminates with examples of the beautiful fractal shapes recursion can produce.
I've enjoyed Al's books in the past, and I'm so glad I checked this one out too. I appreciate the structure, the tone, and the examples in this book the most.
So far, the book is compassionate in its approach to a subject that many learners find intimidating, and is quick to acknowledge when and, more importantly, WHY things might be confusing (e.g. it references "The fact that the call stack doesn't exist in source code is the main reason recursion is so confusing to beginners: recursion relies on something the programmer can't even see!") This type of explanation and acknowledgment help new learners feel at ease.
This book also has great examples in the form of both code (JavaScript and Python) and diagrams/figures. First, the text describes what is happening in an example, then you get to see the code, and then often a diagram or table that visually demonstrates exactly what is happening in the example.
I also appreciate the simplicity of some the examples in the first few Chapters. The book uses common examples (Fibonacci, factorial, exponents, palindromes...) that students likely have already seen (non-recursively) in previous exposures to educational programming content. As someone who doesn't actively think about recursion day-to-day, I appreciate these examples that I can easily wrap my head around.
I also appreciate the "Further Reading" sections, which point readers to deeper or alternative sources covering the relevant material!
All in all, this is another book from Al that I think people should check out if they want to dig deeper into recursion!
As a full-stack developer in the LIS (Library and Information Science) field, Al's work has always been an important cornerstone in my coding education. Data and Systems librarians are often self-taught, and I am no exception. "Automate the Boring Stuff with Python" was my Bible as I took my first steps into the field and I always recommend it to anyone who wants to follow in similar footsteps.
This new title has its own place in my journey: demystifying recursion - a concept that is largely glossed over in any coding classes or bootcamps. Al handles this task with ease, thoroughly covering the gamut with his trademark accessible language, clear examples, and concise writing. For real, there's no fat to trim in this title! The result is a surprisingly easy and enjoyable read; I recommend it to anyone who wants to clear up the mystery of recursion for themselves. Don't worry about getting lost in the sauce; if you know the basics of Python or JavaScript, you'll have no problem following along.
Note: I am writing this review based on a review copy provided by the author. That said, I am purchasing a copy for myself because I enjoyed it so much.
Book was interesting. Covered fractals in Python and Java. References classical recursion algorithms, divide-and-conquer algorithms, memoization (not a typo) & dynamic programming, tail recursion/tail call optimization, permutations/combinations, backtracking/tree traversal algorithms, Karatsuba algorithm. It talks about the difference between recursion and iteration.
The last section went over projects you can do so referenced the fractals, which was probably what I was mainly interested in. That references using the turtle module in Python. This section also references the Droste effect which is a recursive art technique named after an illustration called Droste's Cacao. Talks about the Pillow image library for Python.
Disclaimer: Based on a review copy provided by the author.
Despite what the cutesy cover and title might have you believe, this is not a love letter to recursion, but rather a measured and heavily qualified exploration of the subject. You're not going to read this and start applying recursion everywhere, because the book is careful to show the bad with the good, but you will come away with a better appreciation of the technique and perhaps a new way of thinking about things.
If you're familiar with Al's other books, you know what you're getting here: a friendly guided tour of the presumably unfamiliar. His semi-conversational style makes it a lighter read than you'd anticipate, while still being technical enough to get the core ideas across.
Pros: Demystify recursion; when to use recursion (tree structures and backtracking); how to set up a recursive problem (what is the base case, what argument is passed to the recursive function, how does this argument decrement to the base case); exposure to call stack and stack overflow concepts; good explanation of quicksort and mergesort.
Cons: Javascript examples seem redundant; no solutions to exercises; quite long considering major point is that there are often better approaches than recursion; some overlap with his other books.
I encourage folks learning programming to first buy Al Sweigart’s book, “Automate the Boring Stuff with Python�. That book teaches you Python from the beginning and assumes no previous knowledge. The book used in conjunction with his video course on Udemy, similarly named “Automate the Boring Stuff with Python�, are a great way to start learning Python.
His latest book, "The Recursive Book of Recursion", takes the reader into the more advanced subject of recursion. Topics include the concepts of the stack, pushing in and popping out of the stack, and stack overflow. Recursion is compared to iteration, and examples are shown where both approaches are shown and critiqued. Cases where recursion is advantageous are presented with illustrations to enhance one’s comprehension. While not a beginner’s book, it is a good read to fill in a sufficiently complex subject. Copious Python and JavaScript recursion code are provided. Some fun and cool demonstrations of recursion make use of turtle graphics. Going forward, this will be my ‘go-to� reference for recursion. Thank you, Al Sweigart!
Here’s a breakdown of what’s included (from the book):
Introduction - Covers how to set up Python and JavaScript on your computer to follow along with the code examples in the rest of the book.
Part 1: Understanding Recursion Chapter 1: What is Recursion? - Explains recursion and how it is the natural result of the way programming languages implement functions and function calls. This chapter also argues that recursion isn’t nearly the elegant, mystical concept many claim it is. Chapter 2: Recursion vs. Iteration � Dives into the differences (and many similarities) between recursive and iterative techniques. Chapter 3: Classic Recursion Algorithms � Covers famous recursive programs such as the Tower of Hanoi, the flood fill algorithm, and others. Chapter 4: Backtracking and Tree Traversal Algorithms � Discusses a problem for which recursion is particularly suited: traversing tree data structures, such as when solving mazes and navigating a directory. Chapter 5: Divide-and-Conquer Algorithms � Discusses how recursion is useful for splitting large problems into smaller subproblems and covers several common divide-and-conquer algorithms. Chapter 6: Permutations and Combinations � Covers recursive algorithms involving ordering and matching, as well as the common programming problems to which these techniques are applied. Chapter 7: Memoization and Dynamic Programming � Explains some simple tricks to improve code efficiency when applying recursion in the real world. Chapter 8: Tail Call Optimization � Covers tail call optimization, a common technique used to improve the performance of recursive algorithms, and how it works. Chapter 9: Drawing Fractals � Tours the intriguing art that can be programmatically produced by recursive algorithms. This chapter makes use of turtle graphics for generating its images.
Part 2: Projects Chapter 10: File Finder � Covers a project that searches through the files on your computer according to custom search parameters you provide. Chapter 11: Maze Generator � Covers a project that automatically generates mazes of any size using the recursive backtracker algorithm. Chapter 12: Sliding-Tile Solver � Covers a project that solves sliding-tile puzzles, also called 15-puzzles. Chapter 13: Fractal Art Maker � Explore a project that can produce custom fractal art of your own design. Chapter 14: Droste Maker � Explores a project that produces recursive, picture-in-picture images using the Pillow image-manipulation module Finally, I would also recommend another one of his earlier books, “Cracking Codes with Python�. This is an excellent book on using Python to encrypt, decrypt, and cracking various encryption algorithms.
It may seem odd that a book devoted entirely to recursion would contain such statements as “You never need to use recursion. No programming problem requires recursion â€� In fact, a recursive function might be an overcomplicated solution…â€� Nevertheless, Al Sweigert goes on to explain the ins and outs of a programming technique you should rarely, if ever, need to use. And he does so masterfully. Ìý Recursion and recursive functions seem to many (including me) to be esoteric mysteries, understandable only to those elite few who are wired to think in a pretty unconventional way. We are taught from a young age that you can’t define a word by using that word, itself. To that idea, recursion says “watch me!â€�. Ìý In explaining recursion, Sweigert is careful to begin by laying important groundwork. With adequately (but not overly) detailed and approachable explanations and examples, he discusses functions, their operation and features. He spends significant time explaining the call stack, what it does, how it is structured, and how it operates, leading to a discussion of ‘stack overflowâ€�, one of the risks of using recursion. With that out of the way, he then devotes an entire chapter to comparing recursion and iteration, demonstrating that in the vast majority of cases, recursive functions simply aren’t necessary and in some cases perform worse than their iterative counterparts. Ìý The ensuing chapters look at recursion in action, exploring several popular recursive algorithms and techniques. It is in these chapters where we begin to see where recursion is actually a good idea, where it is a good fit (and also where it is not). So, while recursion may be mostly unnecessary, it is absolutely tailor-made for certain functions. Among other things, Sweigert explores traversing tree structures and demonstrates how memoization (yes, I spelled that correctly) can improve the efficiency of some recursive functions. He provides a plethora of examples demonstrating each concept he explores. It is worth noting here that Sweigert provides code examples in both Python and JavaScript (where possible), making it a valuable resource for students of both. The book ends with several projects that build on the lessons that come before, including one that implements the very cool Droste Effect. Ìý For the beginning and intermediate programmer, which is the only demographic to which I can relate, the real value of this book lies not only in its teaching about recursion. Its value also stems from its ability to stretch you just a little bit further, to make think just a little bit differently, to give you confidence that maybe lofty concepts that seem way above your head are within reach. Any book that can achieve that while simultaneously telling you that what you’re learning is mostly unnecessary is well worth the read.
--Reviewer was provided a review copy by the author
I thoroughly enjoyed working through the code samples accompanied by Al's clear and understandable explanations. There are gems of advice sprinkled throughout the entire book.
The first couple of chapters provides a very clear explanation of recursion and a good comparison of recursion vs. iteration. Understanding is furthered by knowing any recursive algorithm can be performed iteratively using a loop and a stack. Guidance is provided about when and when not to use recursion, and what three features of a programming problem, when presÂent, make it especially suitable to a recursive approach.
Three key questions are answered for each recursive code sample: What is the base case? What argument is passed to the recursive function call? How does this argument become closer to the base case?
Other parts I thoroughly enjoyed: - Seeing how a perfect maze can be represented by graph (DAG) - Quicksort walk-through (Perhaps the best explanation I've ever seen of this classic algorithm.) - Karatsuba Multiplication algorithm and also the supporting math - Introductory set theory refresher - What it means for a function to be "pure" - Memoization and why only pure functions should be memoized - How tail call optimization works
Larger projects follow that provide more opportunities to work through code and see how recursion can be used to succinctly and elegantly implement solutions. I had fun playing with the Sliding-Tile Solver.
Al Sweigart is a gifted programmer who has written several bestselling programming books that help novice programmers develop into burgeoning software engineers; The Recursive Book of Recursion is no exception.
In this book, Sweigart demystifies the recursion algorithm in simple, easy terms that even a beginner will understand. This book includes tons of example code (in both Python and JavaScript), practice questions, and practice projects that will help reinforce concepts discussed in the book.
Before reading this book, I was imitated with the recursion algorithm, and avoided using it. However, after reading this book I was able to design my own programs using the recursion algorithm to solve a few coding challenges.
I found the projects in this book to be fun and engaging. I really enjoyed chapter 14, The Droste Effect, which is recursive art technique that generates a similar recursive image from any photograph or drawing utilizing images. This book really stimulates your imagination of the use of recursion in programming.
Overall, I give this book a five (5) star rating. If you’re curious about learning the recursion algorithm, and want an easy and fun way to learn it, then this book is for you!
To review this book, one must... first... review this book... Recursion jokes aside, this one was a gem. Working through the book rather than just reading it is time-consuming, but that time spent is a huge investment in oneself. What I loved about this book is that the author took the pains to add visual interfaces (albeit plain-text) into the working examples. This additional effort better demonstrates every step of the recursive algorithms. I found that to be very instructive and fulfilling. The projects take the book a notch beyond basic CS. The JavaScript examples were a great addition because there are entirely too many Python books out there. Finally, remember to finish this book... before you can... finish this book. All right, I'll stop now.
I liked learning about backtracking, divide-and-conquer technique, memoization/dynamic programming, and Mandelbrot's ideas (also the joke about his middle initial). I liked that Python and JavaScript was used and featured - Python because it's the best and JavaScript because I didn't have to download anything. My favorite code was the naive recursive exponential function... not sure why - but I didn't try them all. Maybe someday.
Recursion is one of the essential subjects in computer science. Usually, it does not guarantee a more efficient solution, but how you approach the problem with recursion will be helpful in the learning process. In this book, Al Sweigart explains the concept of recursion starting in such a good way that you first learn the basics and how to solve any problem using recursion. Then several projects help the reader to grasp it completely. Reading the author's other books, such as 'Automate The Boring Stuff With Python' would be beneficial in case reader lacks the basics of programming because they should be able to understand the code pieces and have no problem with the fundamentals.