# The New Turing Omnibus Chapter 55 Iteration and Recursion

## Preamble

We christened the new Sheffield Steel breadknife and tucked into some snacks on a rather gloomy Tuesday evening.

## The Meeting

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```
1. 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```
2. 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
end
end
end```
3. 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
end
end```
4. 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.

## Retrospective

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).