TLAC · Think Like A Computer

Advanced programming concepts in TLAC

Recursion and classes, one small step at a time.

Before you begin

This lesson continues 8 Basic Concepts of Programming in TLAC. You only need the ideas of a variable, a condition, a function, and a loop. We will use plain-language instruction cards to learn two new ideas.

  1. Recursion: a function calls itself to continue a job.
  2. Classes: describe a kind of object, with its own information and actions.

Problems 4–8 below keep the original programs’ order and decisions. They are board-game examples for their intended starting positions and board layouts, not general maze solvers. An egg marks a visited square. A move of 2 uses the destination two spaces ahead; these examples assume clear paths.

After each program, compare its initial and final boards, then open the full execution link. Use Next and Previous to follow every instruction, action, and function return, or expand all steps to see every board together. The numbered instructions match the program cards. The turtle picture hides an egg directly underneath it; the egg count still includes that egg.

1. First, without recursion

Start with repetition you already know: a loop. Read all five non-recursive examples first. For each, ask: what repeats, what changes, and what makes it stop? Then compare them with the recursive versions in the next section.

Problem 4 · Follow a route, one space at a time

Turn left once. Each time around the loop, first look for an egg under the turtle. If there is one, end the program. Otherwise, turn right if the way ahead is blocked, place an egg, and move one space. The next loop checks the new square.

Without recursion · Problem 4

0001

Problem 4 — without recursion



0002

Initial action before the loop



0003

Function call: Turn Turtle Left



0004


0005

Repeat until an instruction stops this loop



0006

Check when to stop



0007

If the turtle is standing on an egg, then



0008

Function call: End Program



0009

Leave the innermost loop



0010


0011

Turtle actions



0012

If the turtle cannot move forward 1 space, then



0013

Function call: Turn Turtle Right



0014


0015

Function call: Place an egg under the turtle



0016

Function call: Move Turtle Forward 1 space


Initial board

Eggs on the board: 0

Final board

Eggs on the board: 36

Compare with the recursive version →

Problem 5 · Change the step size to two

This is the same loop pattern as problem 4, but both the movement check and the movement use a distance of 2. Eggs are placed only on the squares where the turtle stands, not on the skipped squares. The stop rule is still finding an egg underneath.

Without recursion · Problem 5

0001

Problem 5



0002

Initial action before the loop



0003

Function call: Turn Turtle Left



0004


0005

Repeat until an instruction stops this loop



0006

Check when to stop



0007

If the turtle is standing on an egg, then



0008

Function call: End Program



0009

Leave the innermost loop



0010


0011

Turtle actions



0012

Use distance 2 for both the check and the movement



0013

If the turtle cannot move forward 2 spaces, then



0014

Function call: Turn Turtle Right



0015


0016

Function call: Place an egg under the turtle



0017

Function call: Move Turtle Forward — distance: 2


Initial board

Eggs on the board: 0

Final board

Eggs on the board: 16

Compare with the recursive version →

Problem 6 · Fill lines, then move to the next line

There are several jobs: reach a wall, fill one line with eggs, then turn and move to the next line. Small inner loops move along a line; the large outer loop repeats the board pattern. Place one final egg when the end of a line is reached.

Without recursion · Problem 6

0001

Problem 6



0002

1. Turn left and go to the wall



0003

Function call: Turn Turtle Left



0004

While the turtle can move forward 1 space, repeat



0005

Function call: Move Turtle Forward 1 space



0006

Function call: Turn Turtle Right



0007


0008

2. Fill the first line before the main loop



0009

While the turtle can move forward 1 space, repeat



0010

Function call: Place an egg under the turtle



0011

Function call: Move Turtle Forward 1 space



0012

Function call: Place an egg under the turtle



0013


0014

3. Main loop for the rest of the board



0015

Repeat until an instruction stops this loop



0016

Inner loop: fill this line with eggs



0017

While the turtle can move forward 1 space, repeat



0018

Function call: Place an egg under the turtle



0019

Function call: Move Turtle Forward 1 space



0020

Function call: Place an egg under the turtle



0021


0022

Turn and move to the next line



0023

If the turtle is facing up, then



0024

Function call: Turn Turtle Right



0025

Function call: Move Turtle Forward 1 space



0026

Function call: Turn Turtle Right



0027

Otherwise



0028

Function call: Turn Turtle Left



0029

Check for the edge of the board



0030

If the turtle is facing right, then



0031

If the turtle cannot move forward 1 space, then



0032

Function call: End Program



0033

Leave the innermost loop



0034


0035

Function call: Move Turtle Forward 1 space



0036

Function call: Turn Turtle Left


Initial board

Eggs on the board: 0

Final board

Eggs on the board: 100

Compare with the recursive version →

Problem 7 · Fill on the upward pass, travel on the return

After the initial line is filled, the outer loop checks the direction. Facing up, it fills a line with eggs. On the downward pass, it moves board height − 1 spaces without placing eggs. It then turns toward the next column. On an empty board, that distance goes from one end of a column to the other.

Without recursion · Problem 7

0001

Problem 7



0002

Function call: Turn Turtle Left



0003


0004

1. Go to the wall ahead



0005

While the turtle can move forward 1 space, repeat



0006

Function call: Move Turtle Forward 1 space



0007


0008

Function call: Turn Turtle Right



0009


0010

2. Fill the first line with eggs



0011

While the turtle can move forward 1 space, repeat



0012

Function call: Place an egg under the turtle



0013

Function call: Move Turtle Forward 1 space



0014

Function call: Place an egg under the turtle



0015


0016

3. Repeat the board pattern



0017

Repeat until an instruction stops this loop



0018

If the turtle is facing up, then



0019

Fill this line with eggs here



0020

While the turtle can move forward 1 space, repeat



0021

Function call: Place an egg under the turtle



0022

Function call: Move Turtle Forward 1 space



0023

Function call: Place an egg under the turtle



0024


0025

Turn after travelling upward



0026

Function call: Turn Turtle Right



0027

Function call: Move Turtle Forward 1 space



0028

Function call: Turn Turtle Right



0029

Otherwise



0030

Move forward by board height minus 1



0031

Function call: Move Turtle Forward — distance: board height − 1



0032

Function call: Turn Turtle Left



0033


0034

Check when to stop



0035

If the turtle is facing right, then



0036

If the turtle cannot move forward 1 space, then



0037

Function call: End Program



0038

Leave the innermost loop



0039


0040

Function call: Move Turtle Forward 1 space



0041

Function call: Turn Turtle Left


Initial board

Eggs on the board: 0

Final board

Eggs on the board: 50

Compare with the recursive version →

Problem 8 · Leave gaps between eggs and columns

First go to the top-left corner, face down, and move down one square. The inner loop places eggs every 2 spaces down or up a column. The outer loop moves across 2 columns and repeats. At the right edge, it checks whether one final sideways step is possible, adjusts the position if so, and ends.

Without recursion · Problem 8

0001

Main instructions: start here



0002


0003

Step A: Go to the left wall (column 0)



0004

Function call: Turn Turtle Left



0005

While the turtle can move forward 1 space, repeat



0006

Function call: Move Turtle Forward 1 space



0007


0008

Step B: Face up and go to the top wall (row 0)



0009

Function call: Turn Turtle Right



0010

While the turtle can move forward 1 space, repeat



0011

Function call: Move Turtle Forward 1 space



0012


0013

Step C: Turn around, face down, and move 1 space to row 1



0014

Function call: Turn Turtle Right



0015

Function call: Turn Turtle Right



0016

Function call: Move Turtle Forward 1 space



0017


0018

Step D: Start the winding route with a loop



0019

Repeat until an instruction stops this loop



0020

1. Place eggs along this column



0021

While the turtle can move forward 2 spaces, repeat



0022

Function call: Place an egg under the turtle



0023

Function call: Move Turtle Forward — distance: 2



0024

Function call: Place the last egg in this column



0025


0026

2. Move to the next column or stop



0027

When the turtle reaches the top facing up



0028

If the turtle is facing up, then



0029

Function call: Turn Turtle Right



0030

Check whether the right edge is too close for 2 spaces



0031

If the turtle cannot move forward 2 spaces, then



0032

If the turtle can move forward 1 space, then



0033

Function call: Move Turtle Forward 1 space



0034

Function call: Turn Turtle Right



0035

Function call: End Program



0036

Leave the innermost loop



0037


0038

Function call: Move Turtle Forward — distance: 2



0039

Function call: Turn Turtle Right



0040


0041

When the turtle reaches the bottom facing down



0042

Otherwise



0043

Function call: Turn Turtle Left



0044

Check whether the right edge is too close for 2 spaces



0045

If the turtle cannot move forward 2 spaces, then



0046

Adjust the final corner position if one more space is available



0047

If the turtle can move forward 1 space, then



0048

Function call: Move Turtle Forward 1 space



0049

Function call: Turn Turtle Right



0050

Function call: End Program



0051

Leave the innermost loop



0052


0053

Function call: Move Turtle Forward — distance: 2



0054

Function call: Turn Turtle Left


Initial board

Eggs on the board: 0

Final board

Eggs on the board: 25

Compare with the recursive version →

2. Now, with recursion

A loop says “do these instructions again.” A recursive function says “do one part, then call this same function for what remains.” Every safe recursive process needs a stopping case (often called the base case) and progress toward that case. The smaller job might be a shorter distance to a wall or fewer unvisited squares.

A definition does not move the turtle. It gives a group of instructions a name. In these recursive charts, begin with the main instructions after the function definitions. A call enters the named function. A call from inside that same function is recursion. Calling a different helper function is an ordinary function call.

Follow the calls: two spaces from a wall

Imagine goToBeginningOfBoard starting two spaces from the wall. Each call keeps a bookmark saying where to continue when its helper finishes. A new call uses the same instructions, but the turtle’s position has already changed.

  1. Call A: 2 spaces remain → move 1 space → call B; A waits.
  2. Call B: 1 space remains → move 1 space → call C; B waits.
  3. Call C: at the wall → cannot move → return to B. This is the base case.
  4. B has nothing left to do → return to A. A also finishes → return to the main instructions.

These waiting calls form a call stack: the last call started is the first to finish. Returning does not undo a move; the turtle stays at the wall. For the board game, put each return bookmark on a small pile of paper slips, and remove the top slip when returning.

Function-name guide
P
one step of the route
goToBeginningOfBoard
go to the wall ahead
fillThisRowWithEggs
fill this line with eggs
fillThisColumnWithEggs
place eggs every two spaces in this column
repeatThisUntilBoardFull
repeat the board pattern

Problem 4 · Follow a route, one space at a time

P contains one pass through the same actions. Its last instruction calls P again, now with the turtle on a new square. The call inside P is the recursive call. The stopping case is standing on an egg; End Program stops the entire run.

With recursion · Problem 4

0001

Problem 4



0002

Function definition: P



0003

If the turtle is standing on an egg, then



0004

Function call: End Program



0005

If the turtle cannot move forward 1 space, then



0006

Function call: Turn Turtle Right



0007

Function call: Place an egg under the turtle



0008

Function call: Move Turtle Forward 1 space



0009

Function call: P



0010

Function call: Turn Turtle Left



0011

Function call: P


Find the egg check before the next move. Without it, returning to a visited square would not stop the route.

Initial board

Eggs on the board: 0

Final board

Eggs on the board: 36

← Revisit the loop version

Problem 5 · Change the step size to two

P again calls itself after moving. The repeated action now advances 2 spaces, but the recursive structure has not changed. A distance check of 1 would not establish that a move of 2 stays on the board.

With recursion · Problem 5

0001

Problem 5



0002

Function definition: P



0003

If the turtle is standing on an egg, then



0004

Function call: End Program



0005

If the turtle cannot move forward 2 spaces, then



0006

Function call: Turn Turtle Right



0007

Function call: Place an egg under the turtle



0008

Function call: Move Turtle Forward — distance: 2



0009

Function call: P



0010

Function call: Turn Turtle Left



0011

Function call: P


Compare the two distance values. They must describe the same intended move.

Initial board

Eggs on the board: 0

Final board

Eggs on the board: 16

← Revisit the loop version

Problem 6 · Fill lines, then move to the next line

Three named functions take over those jobs. goToBeginningOfBoard moves and calls itself until it reaches a wall. fillThisRowWithEggs places eggs and calls itself until the line ends, then places the last egg and returns. repeatThisUntilBoardFull calls the filling function, changes lines, and calls itself again until End Program is reached.

With recursion · Problem 6

0001

Problem 6



0002

Function definition: goToBeginningOfBoard



0003

If the turtle can move forward 1 space, then



0004

Function call: Move Turtle Forward 1 space



0005

Function call: goToBeginningOfBoard



0006

Otherwise



0007

Return to the instruction after the call



0008

Function definition: fillThisRowWithEggs



0009

If the turtle can move forward 1 space, then



0010

Function call: Place an egg under the turtle



0011

Function call: Move Turtle Forward 1 space



0012

Function call: fillThisRowWithEggs



0013

Otherwise



0014

Function call: Place an egg under the turtle



0015

Return to the instruction after the call



0016

Function definition: repeatThisUntilBoardFull



0017

Function call: fillThisRowWithEggs



0018

If the turtle is facing up, then



0019

Function call: Turn Turtle Right



0020

Function call: Move Turtle Forward 1 space



0021

Function call: Turn Turtle Right



0022

Otherwise



0023

Function call: Turn Turtle Left



0024

If the turtle is facing right, then



0025

If the turtle cannot move forward 1 space, then



0026

Function call: End Program



0027

Function call: Move Turtle Forward 1 space



0028

Function call: Turn Turtle Left



0029

Function call: repeatThisUntilBoardFull



0030

Function call: Turn Turtle Left



0031

Function call: goToBeginningOfBoard



0032

Function call: Turn Turtle Right



0033

Function call: fillThisRowWithEggs



0034

Function call: repeatThisUntilBoardFull


These supplied examples fill the first line before the main repetition and visit its final square again at the start of that repetition. In TLAC, placing an egg marks a square as occupied; it does not stack another egg there. The function name says “Row”, but the turtle travels vertically here, along a column.

Initial board

Eggs on the board: 0

Final board

Eggs on the board: 100

← Revisit the loop version

Problem 7 · Fill on the upward pass, travel on the return

The outer function still calls itself, but its direction check chooses which work to do first. The upward branch calls fillThisRowWithEggs; the other branch makes the long move. The filling helper returns at the wall, while the overall pattern ends at the right-edge check.

With recursion · Problem 7

0001

Problem 7



0002

Function definition: goToBeginningOfBoard



0003

If the turtle can move forward 1 space, then



0004

Function call: Move Turtle Forward 1 space



0005

Function call: goToBeginningOfBoard



0006

Otherwise



0007

Return to the instruction after the call



0008

Function definition: fillThisRowWithEggs



0009

If the turtle can move forward 1 space, then



0010

Function call: Place an egg under the turtle



0011

Function call: Move Turtle Forward 1 space



0012

Function call: fillThisRowWithEggs



0013

Return to the instruction after the call



0014

Otherwise



0015

Function call: Place an egg under the turtle



0016

Return to the instruction after the call



0017

Function definition: repeatThisUntilBoardFull



0018

If the turtle is facing up, then



0019

Function call: fillThisRowWithEggs



0020

Function call: Turn Turtle Right



0021

Function call: Move Turtle Forward 1 space



0022

Function call: Turn Turtle Right



0023

Otherwise



0024

Function call: Move Turtle Forward — distance: board height − 1



0025

Function call: Turn Turtle Left



0026

If the turtle is facing right, then



0027

If the turtle cannot move forward 1 space, then



0028

Function call: End Program



0029

Function call: Move Turtle Forward 1 space



0030

Function call: Turn Turtle Left



0031

Function call: repeatThisUntilBoardFull



0032

Function call: Turn Turtle Left



0033

Function call: goToBeginningOfBoard



0034

Function call: Turn Turtle Right



0035

Function call: fillThisRowWithEggs



0036

Function call: repeatThisUntilBoardFull


Recursion can repeat a decision, not just a single movement. Notice which branch places eggs and which branch only moves.

Initial board

Eggs on the board: 0

Final board

Eggs on the board: 50

← Revisit the loop version

Problem 8 · Leave gaps between eggs and columns

fillThisColumnWithEggs calls itself after each 2-space move. Its stopping case places the final egg and returns when another 2-space move is impossible. repeatThisUntilBoardFull handles the turns and calls itself for the next column. If moving across 2 columns is impossible, the program ends instead of making another recursive call.

With recursion · Problem 8

0001

1. Move the turtle to the wall ahead



0002

Function definition: goToBeginningOfBoard



0003

If the turtle can move forward 1 space, then



0004

Function call: Move Turtle Forward 1 space



0005

Function call: goToBeginningOfBoard



0006

Otherwise



0007

Return to the instruction after the call



0008


0009

2. Place an egg, move 2 spaces, and call this function again



0010

Function definition: fillThisColumnWithEggs



0011

If the turtle can move forward 2 spaces, then



0012

Function call: Place an egg under the turtle



0013

Function call: Move Turtle Forward — distance: 2



0014

Function call: fillThisColumnWithEggs



0015

Otherwise



0016

Function call: Place an egg under the turtle



0017

Return to the instruction after the call



0018


0019

3. Turn, move across 2 columns, and repeat



0020

Function definition: repeatThisUntilBoardFull



0021

Function call: fillThisColumnWithEggs



0022


0023

When the turtle reaches the top facing up



0024

If the turtle is facing up, then



0025

Function call: Turn Turtle Right



0026

Check whether the right edge is too close for 2 spaces



0027

If the turtle cannot move forward 2 spaces, then



0028

If the turtle can move forward 1 space, then



0029

Function call: Move Turtle Forward 1 space



0030

Function call: Turn Turtle Right



0031

Function call: End Program



0032

Return to the instruction after the call



0033


0034

Function call: Move Turtle Forward — distance: 2



0035

Function call: Turn Turtle Right



0036


0037

When the turtle reaches the bottom facing down



0038

Otherwise



0039

Function call: Turn Turtle Left



0040

Check whether the right edge is too close for 2 spaces



0041

If the turtle cannot move forward 2 spaces, then



0042

Adjust the final corner position if one more space is available



0043

If the turtle can move forward 1 space, then



0044

Function call: Move Turtle Forward 1 space



0045

Function call: Turn Turtle Right



0046

Function call: End Program



0047

Return to the instruction after the call



0048


0049

Function call: Move Turtle Forward — distance: 2



0050

Function call: Turn Turtle Left



0051


0052

Call this function again for the new column



0053

Function call: repeatThisUntilBoardFull



0054


0055


0056

Main instructions: start here



0057


0058

Step A: Go to the left wall (column 0)



0059

Function call: Turn Turtle Left



0060

Function call: goToBeginningOfBoard



0061


0062

Step B: Face up and go to the top wall (row 0)



0063

Function call: Turn Turtle Right



0064

Function call: goToBeginningOfBoard



0065


0066

Step C: Turn around, face down, and move 1 space to row 1



0067

Function call: Turn Turtle Right



0068

Function call: Turn Turtle Right



0069

Function call: Move Turtle Forward 1 space



0070


0071

Step D: Start the winding route with recursive calls



0072

Function call: repeatThisUntilBoardFull


There are two different stopping questions: “Can I continue this column?” and “Can I reach another column?” Each repeated job needs its own answer.

Initial board

Eggs on the board: 0

Final board

Eggs on the board: 25

← Revisit the loop version

Which should you choose? For a simple repeated movement, a loop is often easier to follow. Recursion is useful when the remaining work naturally resembles the original job. Each unfinished call needs a return bookmark, so very deep recursion can use too much memory. These examples teach the idea; recursion does not automatically make a program faster.

3. Classes and objects

A class describes a kind of thing: the information it keeps and the actions it can perform. Here our class describes a turtle. An object is one particular turtle made from that description, also called an instance. Aki and Sol can be two objects of the same Turtle class.

Class: Turtle

  1. Attributes (stored information): name, horizontal position X, vertical position Y, and direction.
  2. Constructor (initial setup): receive a name and starting X and Y; create this turtle there, facing up.
  3. Method “Move forward”: move this turtle one square in its own direction.
  4. Method “Turn right”: rotate this turtle 90° clockwise; its position stays the same.
  5. Method “Place an egg”: place an egg on this turtle’s current square.
  6. Method “Mark and move”: ask this turtle to place an egg, then ask it to move forward. One method can use other methods.

An attribute is a named piece of information belonging to an object. A method is a function provided by its class. “This turtle” means the object receiving the request: asking Aki to move changes Aki’s position. The class definition alone does not create a turtle or move it. Creating an object runs its constructor, not all of its methods.

These examples use a 6 × 6 TLAC board. X increases to the right; Y increases downward. The top-left square is X=0, Y=0. The paths below stay inside the board. The full executions show every method request and the individual turtle actions it causes.

Example 1: one turtle leaves an egg trail

Create Aki at X=1, Y=4, facing up. Ask Aki to mark and move twice, turn right, and mark and move once more. The two upward moves change Y from 4 to 2. The final move changes X from 1 to 2. Aki finishes at X=2, Y=2, facing right, with three eggs left behind.

Program: Aki’s trail

0001

Create turtle Aki at X=1, Y=4, facing up



0002

Ask Aki: mark this square and move forward



0003

Ask Aki: mark this square and move forward



0004

Ask Aki: turn right



0005

Ask Aki: mark this square and move forward


Initial board

Eggs on the board: 0

Final board

Eggs on the board: 3

Example 2: two objects, independent positions

Start a fresh board. Create Aki at X=1, Y=4 and Sol at X=4, Y=4. Both face up. Ask Aki to mark and move twice. Sol has not moved! Now ask Sol to turn right and mark and move once. Aki stays at X=1, Y=2, facing up; Sol finishes at X=5, Y=4, facing right.

Program: Aki and Sol

0001

Create turtle Aki at X=1, Y=4, facing up



0002

Create turtle Sol at X=4, Y=4, facing up



0003

Ask Aki: mark this square and move forward



0004

Ask Aki: mark this square and move forward



0005

Ask Sol: turn right



0006

Ask Sol: mark this square and move forward


Initial board

Eggs on the board: 0

Final board

Eggs on the board: 3

Same class, separate state. Aki and Sol use the same methods, but each stores its own name, X, Y, and direction. They share the board, so both can see eggs placed on it. An egg belongs to a board square, not to the turtle’s personal attributes. The execution page lists both turtles’ states so you can see which one changes.

Calling “Mark and move” uses other methods, but it is not recursion: that method never calls itself. Classes organize information and actions. Recursion is a way for a function to repeat work. A method can be recursive, but it does not have to be.

4. Try it yourself

1. What if the move toward the wall is removed?

Show an answer

If the turtle starts away from the wall, the next call sees the same position. It keeps calling without reaching the base case. A stopping condition alone is not enough: the repeated action must make progress.

2. Does returning from the column-filling function end the entire program?

Show an answer

No. Return resumes the caller. That caller may turn the turtle and start another column. End Program is the instruction that stops everything.

3. Aki and Sol face up. Ask only Sol to turn right. Does Aki turn too?

Show an answer

No. Sol faces right; Aki still faces up. Each turtle object stores its own direction attribute.

4. Design a “Turn around” method for the Turtle class.

Show an answer

Ask this turtle to turn right twice. Its direction changes by 180°, while X and Y stay unchanged. Another turtle on the board should keep its own direction.

Remember: recursion needs a stopping case and progress toward it. A class describes attributes and methods; each object has its own state. You can trace both ideas with paper, coins, and a few instruction cards.

All program executions

A companion lesson to TLAC (Think Like A Computer), created by Usmar A. Padow (amigojapan). The recursion charts use the supplied problem 4–8 programs. amigojapan’s homepage.

Back to top