How to make your computer emulate a computer.

While you may not have access to a physical Turing machine, that should’nt stop you from simulating a Turing machine with… your computer! I will cover how Turing machines function, and Python code to build a simple one to test for alleged palindromes. I know that programs that check for palindromes are common exercise for programming beginners, and that you could simply check online or even copy some code in your favorite language. However, let’s pretend that deep down you would *really* like to use a Turing machine for the task.

This article is inspired by Chapter 5 of *Perspectives in Computation* by Robert Geroch — Amazon.

**You can find the complete code on GitHub here.**

## What is a Turing machine?

A Turing machines consists of three parts:

- A tape.
- A write head
- A machine state.

The tape is divided into a sequence of squares, each of which may store a single character belonging to a given character set.

## That didn’t answer much

Why are Turing machines interesting? You’ve definitely heard of them already, so let’s just copy it quickly from Turing’s own words and leave further details to Geroch’s chapter referenced above.

*It is possible to invent a single machine which can be used to compute any computable sequence.*

A Turing machine can simulate anything, given unlimited storage space!

## How do Turing machines function?

The machine works based on a table of rules. At any given step, the write head is over some square on the tape. At this time point, the machine: (1) reads that square of the tape and (2) reads the state that the machine is in. The machine then looks in the table of rules to determine: (1) which new character to write, (2) which new state to go to, and (3) which direction, one square left or right, the head should move. Of course, the machine is allowed to print the same character and maintain the same state.

When has the machine finished? A special state is reserved for this purpose. Once the machine enters this state, the program has finished running.

Technically, we will make several simplifcations to the Turing machine. First, we will assume we have access at all times to the machine’s state, and can simply measure whether or not it has finished. That is, there will be no need for the machine to write on the tape to indicate it has finished. As for the code, if you will, ignore the inefficiencies and focus on the function.

#turing-machine #python #programming #computer-science #computing