The Trick Of The Mind - Programming & Algorithmic Thought
Written by Mike James
Friday, 03 September 2021
Article Index
The Trick Of The Mind - Programming & Algorithmic Thought
Following Instructions
Is Programming The Same As Giving Instructions?

English, or any other natural language, is great for descriptions but when you start to use any such language prescriptively you quickly see problems of imprecision.

When you are writing a poem describing the sea, does it matter if the description is vague? In poetry, keeping descriptions vague is often a good idea because it allows the reader to exercise imagination. You could say that being vague is part of the art of creative writing, but it isn't so much part of creative programming.

Take the first part of our example:

First walk forward, turn sharp right and...

What does "sharp right" mean? Clearly the intention is that it is a right angle or 90 degrees but it isn't 100% clear and different people might choose different interpretations producing different results.

This isn't good if you want to be sure that the person doing the walking ends up back at the same spot.

What we need is a precise statement of what is to be done and for this we need to spend more effort setting down exactly what is to happen.

For example:

walk forward 10 units, turn 90 degrees right, walk forward 10 units , turn 90 degrees right, walk forward 10 units, turn 90 degrees right, walk forward 10 units.

is an unambiguous set of instructions. It can be difficult sometimes to know when a set of instruction is precise and this is something else we need to come back to. You could say, for example, that it isn't clear what 10 units are and what if the person doesn't know what 90 degrees is? For the moment we have to insist that the person does know what a 90-degree turn means, or if not they need to go and find out.

This form of expression may be precise but it is tedious - so much writing!

Anyone finding themselves in the position of having to write "walk forward n units" over and over again would probably resort to an abbreviation:

FW10 = Forward 10 Units

Once you have one abbreviation why not keep inventing them. For turn 90 degrees right you could just use RT90.  Now you can write the instruction as:

`FW10 RT90 FW10 RT90 FW10 RT90 FW10`

It means the same thing and this is your first program in our programming language giving the instruction to go forward 10, right turn, forward 10, right turn, forward 10, right turn, forward 10.

Yes, it is that easy.

However, don't fall into the trap of thinking that it really is trivial. This is a sophisticated thing to do. It only seems easy because of the trick of the mind that makes it easy.

Later you will see how this simple idea gets increasingly elaborate. If you follow its development step by step then it is always easy but if you are thrown into its later stages without preparation it is a frightening mess. After all what would you make of the program

FW10RT90FW10RT90FW10RT90FW10

if it had been presented to you at the start of the chapter without any explanation and the question posed - what does this do?

Codes and languages that you make up yourself are very easy to understand. Codes and languages that other people make up are much harder!

Of course, once you know what FW and RT mean then it all becomes simple again.

In case you think that this example is a bit contrived then it is worth mentioning that these commands are part of a full computer language called Logo - i.e. this is not a particularly contrived example.

## A Computer Language for Drawing

You can also see that with some slight extensions the language can be used to create  range of different shapes.

In fact what we have is a computer language for drawing. Imagine that the person walking has a pen or a paint brush. You could tell them to put the brush down onto a surface and so make a mark as they move or pick the brush up and move without making a mark.

The commands, again from the Logo computer language are:

PENDOWN

and

PENUP

So with these additions, what does the following draw:

PENDOWN
FW 10
PENUP
RT 90
FW 10
RT 90
PENDOWN
FW 10
PENUP
RT 90
FW 10

If you find it hard to work out get a pen and follow the instructions.

You will find that it draws two parallel lines, i.e two opposing sides of a square.

From this small addition you can see that programming does get more difficult to follow as it gets more complex.

It is a matter of practice, however.

Once you have the essential idea that a set of static texts can actually be something dynamic if you read it as a set of imperative commands then you have the main idea of programming - the rest is practice and familiarity.

Of course, the few commands that we have introduced are not enough to do everything we could possibly want, but it is a start. There are some key concepts that we need to tackle to get to the full capabilities of a programming language. In fact because the language that we have so far is so restrictive and simple it is often referred to as a "little language" or in a more recent jargon a Domain Specific Language. Most full programming languages have a number of little languages within them. For example, the drawing language that we have started to introduce is often part of a bigger language.

## Arithmetic as a little language

Another little language is arithmetic. When you write 2+3*4 (most programming languages use* for the multiplication sign) you have written a program that tells someone how to perform some arithmetic, i.e. multiply 3 by 4 and add 2, the arrive at the answer 14.

Notice that in this case the order in which you have to do things isn't the order in which the instructions have been written. If you thought that the instruction was to add 3 to 2 and then multiply by 4, i.e. answer 20, you have identified the reason you have been finding arithmetic and math in general difficult.

Arithmetic is a slightly odd little language and it is this out-of-order nature that make it so difficult for children to get to grips with at first. It is also the initial connection between the field of mathematics and programming. Arithmetic notation is a special little language and one of the first formalised little languages that we generally meet in the real world. How well you take to understanding arithmetic, and later and more importantly algebra, which is just arithmetic with symbols, is a good indicator of how easy you will find programming.

Sometimes, however, it works the other way. Only after having the fact that arithmetic and algebra are just other little languages, do some people suddenly see the light and are able to "do math" - another advantage of learning to program.

To be clear, however, mathematical thinking doesn't stop at arithmetic but it does start with arithmetic notation.

That is, you might not be able to do the additions and the multiplications but if you can read and understand the little language that is arithmetic/algebra you are a mathematician.

Pocket calculators and spreadsheets DO arithmetic; mathematicians know how to tell them to do it using the arithmetic little language.

Last Updated ( Friday, 03 September 2021 )