Enter the maze

The Abracadabra program: the importance of spelling it out

by Paul Curzon, Queen Mary University of London

An Abracadabra Spell. PC

Does magic really work? With a few words you make a things happen in the world. The idea of a magic spell is that if you follow the instructions in the spell exactly, the effect you are after happens. Sadly, spells have never worked. There are powerful words that can do much the same though: algorithms written out as programs in a programming language. They are instructions to be followed blindly by a computer that guarantees some effect whether in a computer or in the world. In a mere tens of years they have completely transformed our world.

Spells were arguably an early attempt at algorithms, their words were supposed to have a guaranteed effect just like algorithms. Just as alchemy almost had the right idea that ultimately led to chemistry, magic spells perhaps almost had the right idea that ultimately led to programs. Its just a shame for budding magicians they never actually worked at all.

Of course part of the problem, as magicians and alchemists alike realised, was how hard it was for mere humans to follow instructions exactly. When nothing happened, they assumed the spells were right, it was just they weren't good enough at following them exactly. Or, perhaps the reason it didn't seem to work was that the instructions weren't clear enough to know exactly what they were meant to do! If only they could follow the steps exactly then perhaps the magic would have worked.

The most famous magic spell of all is 'abracadabra'. It started out as a Roman spell to ward off Malaria: a nasty disease, spread by mosquitos that you don't want to get. You didn't just speak the words though, you had to follow the instructions:

Write out the word repeatedly on a piece of paper, losing a letter each time so they form a triangle. Next, put the paper in an amulet and wear it to ward off disease.

The idea lasted for a thousand or so years - it was even written on doors in London during the Great Plague. Right idea, but the wrong instructions! The actual way to ward off Malaria is to take anti-malaria drugs, wear insect repellant and always sleep under a mosquito net. of course.

However, when someone died of malaria they thought it could just have been that they didn't always quite follow the instruction properly. Those instructions in the abracadabra spell, aren't that clear. Which letter do you lose? Do you write them out in a right angled triangle or an isosceles one? Even with such apparently simple instructions it isn't completely clear.

Here is one way to do it...

A B R A C A D A B R A
A B R A C A D A B R
A B R A C A D A B
A B R A A C D A
A B R A C A D
A B R A C A
A B R A C
A B R A
A B R
A B
A 

Here is another ...

A B R A C A D A B R A
 A B R A C A D A B R
  A B R A C A D A B
   A B R A A C D A
    A B R A C A D
     A B R A C A
      A B R A C
       A B R A
        A B R
         A B
          A 

Which, if either, is right?

Precise instructions matter if you are to get it right. Programs aren't magic, and they work because they are instructions that computers can follow, but the early computer scientists realised something really important - they needed a special language to make it work. It wasn't a magical language they needed, but one where the meaning of every word, every command, was absolutely clear and precise. They needed to be spelled out not with magic but with mathematical precision, so a dumb computer with no intelligence could just blindly follow the steps. That is what programming languages give you.

Magic words once seemed important. Programs really are.

A Python Abracadabra program

It still won't ward off malaria, but here is a program that spells out precisely a way to print out an abracadabra triangle written in the programming language Python. We explain how it works step by step here:

# A Python program that prints out the word abracadabra in a triangle
# This is the way it supposedly should be written to protect from disease:
# write it over and over again in a triangle losing a letter each time, then
# put the written pattern in an amulet and wear it round your neck.

def printletter(letter):
        """Print a single letter followed by a space and no newline"""
        print(letter, end=" ")

def printline(word):
        """Print a word out with spaces between each letter"""
        for letter in word:
                printletter(letter)
        print()

def triangle(word):
	"""Print a given word in a magic triangle removing the last letter each time it is written"""
	spaces = ""
	for currentend in range(len(word), 0, -1):
		print(spaces, end="")
		printline(word[0:currentend])
		spaces = spaces + " "

def abracadabra():
	"""Print ABRACADABRA in a magic triangle"""
	triangle("ABRACADABRA")
  
abracadabra()

Why not invent your own spell words that need to be written in particular patterns, then write programs to write them exactly right every time.