Pseudocode Poems

Pseudocode poems are poems that work both as an algorithm/program and as a poem.

Poems often use the ambiguity in language and aim to affect emotions. Pseudocode is intended to be precise. Programs certainly are. They do something specific and have a single precise meaning.

Use pseudocode poems, for example, to start discussion about the meaning/semantics of programming constructs and how they differ from english use of words. In particular encourage students to think about how words in programs have very precise meanings. They can also be used to prompt discussion of the importance of practicing reading code.

The idea was inspired by Bryan Bilston’s poem ‘Two Paths Diverged’. Read it here or buy his wonderful book of poems, ‘ You took the last bus home’

Pseudocode poems incorporate sequencing, selection or repetition constructs and other kinds of statements. You can implement them as an actual program. Below  are our attempts. Can you write one?

Repetition

Is this poem really long?

it is true
while it is true
    this is short
it is endless

                Paul Curzon

Here it is implemented as a Python program.

def isthispoemreallylong():
    """Is this poem really long?"""
    it = True
    while (it == True) :
        this = 'short'
    it = 'endless'

isthispoemreallylong()

Can you work out what it does as an algorithm/program, rather than as just a normal poem? You may need a version with print statements to understand its beauty.

def isthispoemreallylong2():
    """Is this poem really long?"""
    it = True
    while (it == True) :
        this = 'short'
        print("this is " + this)
    it = 'endless'
    print("it is " + it)

isthispoemreallylong2()

Discussion

In the pseudocode of this poem the verb to be is used for two different purposes: as assignment statement (it is true is used as a statement to mean it = True) and as a boolean expression (it is true is used as a boolean expression to mean it == True). As an assignment it is used as a command to do something. As an expression it is something representing a value: it is either true or false

Nouns become values: the actual data stored in variables. Pronouns in this poem become variables, sometimes where but actually mean whatever their current value set by the last assignment is.

The word while indicates the start of a while loop. In the pseudocode, it is a command to repeat the following statement(s). It checks the boolean statement after the body is executed each time. Only if that boolean expression is false does it stop. In this case the body sets the variable named this to string value ‘short’. The test is about a different variable though which is never changed inside the loop, so once in the loop there is nothing that will ever change the test. Variable it will always be True and the loop will keep setting variable this to value ‘short’, over and over again. This means the loop is a non-terminating loop. It never exits so the lines of code after it are never executed – in effect never read so never followed by the computer. The variable it is never set to the value ‘endless’.

Overall the poem is short in number of lines but is endless if executed. It is the equivalent of a poem that once you start reading it you never get to the end:

it is true 
check it is true 
this is short
check it is true 
this is short
check it is true 
this is short
...

Selection

Here is a pseudocode poem based on selection.

Violets are violet
if roses are red and violets are blue
then
    Life is sweet
else
    I love you 

                                        Paul Curzon

Here it is implemented as a Python program (with appropriate initialisation).

def violetsareviolet():
	"""Violets are violet"""
	roses = 'red'
	violets = 'violet'
	
	if roses == 'red' and violets == 'blue':
		print('Life is sweet')
	else:
		print('I love you')

violetsareviolet()


Sequencing

Here is a pseudocode poem based on sequencing.

What am I when it’s all over?

I am dire.
I am fire.

I am alone.
I am stone.

I am old.
I am cold.
                                        Paul Curzon

Here it is implemented as a Python program.

def whatamI():
	"""What am I when it's all over?"""

	I = 'dire'
	I = 'fire'
	
	I = 'alone'
	I = 'stone'
	
	I = 'old'
	I = 'cold'
	
	print(I)

 

The poem is about a volcano, but the answer to the question in the title is ‘cold’. [Highlight the blanks to read the answer]

More ideas mixing english and computing


IoC logo on white