-
Notifications
You must be signed in to change notification settings - Fork 15
The New Turing Omnibus Chapter 55 Iteration and Recursion
We christened the new Sheffield Steel breadknife and tucked into some snacks on a rather gloomy Tuesday evening.
We began by discussing what we should do in the meeting. Tom suggested it might be useful to begin by going through the Towers of Hanoi problem mentioned in the chapter. We found objects of different sizes and Paul shuffled them around to demonstrate a solution to the three-disk problem.
We discussed the different ways of thinking about the problem. There was a split between people who seemed to naturally think about the problem recursively while others thought about the iterative algorithm (or some variation of it).
Tom guided us through the recursive algorithm on the board.
- Tower of Hanoi
- Number of moves is 2n - 1 as the solution can be seen as a binary tree
We noted that recursive structure of the problem produced a tree. We reasoned that when the leaf nodes are ignored, this is a binary tree, which explains where the 2n - 1 steps comes from. We also noted that the recursive algorithm would move disks in an order corresponding to a depth-first traversal of the tree.
We tackled the first problem in the chapter to produce a proof by induction that there must be at least 2n -1 moves for the given Hanoi algorithm:
After this, we moved on to think more generally about iterative and recursive algorithms and how you can convert between the two. James noted that recursive algorithms can always be converted to iterative ones – "that's how computers work".
Going in the other direction wasn't so obvious. Tom walked us through a set of mechanical steps that could (more-or-less) be followed to carry out this conversion.
Starting with the following iterative code:
def sum(array)
total = 0
i = 0
while i < array.length
total = total + array[i]
i = i + 1
end
total
end
-
Turn local variables into default arguments (chop off head):
def sum(array, total = 0, i = 0) while i < array.length total = total + array[i] i = i + 1 end total end
-
Turn loop condition into a conditional return (chop off tail)
def sum(array, total = 0, i = 0) loop do if i < array.length total = total + array[i] i = i + 1 else return total end end end
-
Replace next loop iteration with recursive call
def sum(array, total = 0, i = 0) if i < array.length total = total + array[i] i = i + 1 return sum(array, total, i) else return total end end
-
Refactor
def sum(array, total = 0, i = 0) if i < array.length sum(array, total + array[i], i + 1) else total end end
We decided to try and follow these steps for the Towers of Hanoi problem. Before we could do that, we needed to implement the iterative algorithm for solving Towers of Hanoi presented in the book. Paul dared to drive and after a brief discussion about Vim undo files we got stuck into implementing the iterative algorithm.
...
...
... to much success!
We then worked through Tom's steps to convert the algorithm to a recursive one.
We held a short retrospective at the end of the meeting. We discussed the CFP and voting process and noted that there isn't much time between meetings and this often feels hurried.
To address this, we decided to decouple the CFP/voting process from the meeting organisation process and have a persistent #topics channel in Slack that allows people to vote at any time for a chapter (or other proposal).
- Our iteration and recursion code from the meeting
- Chris's implementation using a single array
- James’s recursive Haskell and Ruby implementations
## Thanks
Thanks to Leo and Geckoboard for hosting, Richard for working through the proof by induction and Tom for preparing his steps to convert an algorithm from iterative to recursive.
- Home
- Documentation
- Choosing a Topic
- Shows & Tells
- Miscellaneous
- Opt Art
- Reinforcement Learning: An Introduction
- 10 Technical Papers Every Programmer Should Read (At Least Twice)
- 7 More Languages in 7 Weeks
- Lua, Day 1: The Call to Adventure
- Lua, Day 2: Tables All the Way Down
- Lua, Day 3
- Factor, Day 1: Stack On, Stack Off
- Factor, Day 2: Painting the Fence
- Factor, Day 3: Balancing on a Boat
- Elm, Day 1: Handling the Basics
- Elm, Day 2: The Elm Architecture
- Elm, Day 3: The Elm Architecture
- Elixir, Day 1: Laying a Great Foundation
- Elixir, Day 2: Controlling Mutations
- Elixir, Day 3: Spawning and Respawning
- Julia, Day 1: Resistance Is Futile
- Julia, Day 2: Getting Assimilated
- Julia, Day 3: Become One With Julia
- Minikanren, Days 1-3
- Minikanren, Einstein's Puzzle
- Idris Days 1-2
- Types and Programming Languages
- Chapter 1: Introduction
- Chapter 2: Mathematical Preliminaries
- Chapter 3: Untyped Arithmetic Expressions
- Chapter 4: An ML Implementation of Arithmetic Expressions
- Chapter 5: The Untyped Lambda-Calculus
- Chapters 6 & 7: De Bruijn Indices and an ML Implementation of the Lambda-Calculus
- Chapter 8: Typed Arithmetic Expressions
- Chapter 9: The Simply-Typed Lambda Calculus
- Chapter 10: An ML Implementation of Simple Types
- Chapter 11: Simple Extensions
- Chapter 11 Redux: Simple Extensions
- Chapter 13: References
- Chapter 14: Exceptions
- Chapter 15: Subtyping – Part 1
- Chapter 15: Subtyping – Part 2
- Chapter 16: The Metatheory of Subtyping
- Chapter 16: Implementation
- Chapter 18: Case Study: Imperative Objects
- Chapter 19: Case Study: Featherweight Java
- The New Turing Omnibus
- Errata
- Chapter 11: Search Trees
- Chapter 8: Random Numbers
- Chapter 35: Sequential Sorting
- Chapter 58: Predicate Calculus
- Chapter 27: Perceptrons
- Chapter 9: Mathematical Research
- Chapter 16: Genetic Algorithms
- Chapter 37: Public Key Cryptography
- Chapter 6: Game Trees
- Chapter 5: Gödel's Theorem
- Chapter 34: Satisfiability (also featuring: Sentient)
- Chapter 44: Cellular Automata
- Chapter 47: Storing Images
- Chapter 12: Error-Correcting Codes
- Chapter 32: The Fast Fourier Transform
- Chapter 36: Neural Networks That Learn
- Chapter 41: NP-Completeness
- Chapter 55: Iteration and Recursion
- Chapter 19: Computer Vision
- Chapter 61: Searching Strings
- Chapter 66: Church's Thesis
- Chapter 52: Text Compression
- Chapter 22: Minimum spanning tree
- Chapter 64: Logic Programming
- Chapter 60: Computer Viruses
- Show & Tell
- Elements of Computing Systems
- Archived pages