Writing Love Letters, Christmas Greetings and Postcards by algorithm

Create a creative algorithm that writes love letters or christmas card greetings to learn about how programs can be creative and learn about english grammar and writing. Do it as unplugged programming using index cards. For classes that can program, have them then write the program. 

The first ever attempt to write a creative program was probably written by Christopher Strachey in the 1950s. It was a love letter writing program that wrote very flowery, gushing letters such as:


Read more about it and Christopher Strachey in the cs4fn articles here: and see a version of it in action here [EXTERNAL] as well as see the word lists Strachey used.

Slides for my talk at I Love Computing 2023 are here [pdf].

Activity: Love Letter writer

Write your own version of the love letter program. By changing the templates and word lists perhaps you can improve on Strachey’s version. Here is one of the offerings from my attempt to write a love letter writing program:

Beloved Little Cabbage,

I cling to your anxious fervour. I want to hold you forever. You are my fondest craving. You are my fondest enthusiasm. My affection lovingly yearns for your loveable passion.

Yours, keenly Q

Have students work out the templates Strachey and/or I used. Mine were chosen randomly from:

salutation1 + ” ” + salutation2 + “,”

“I ” + verb + ” your ” + adjective + ” ” + noun + “.”

“You are my ” + noun + “.”

“I want ” + verb + ” you forever.”

“I ” + verb + ” your ” + adjective + ” ” + noun + “.”

“My ” + noun1 + ” ” + adverb + ” ” + verb  + ” your ” + adjective + ” ” + noun2 + “.”

“Yours, ” + adverb + ” Q”

Have them work then out their own templates and word lists to fit them.

Write out the templates (one for each sentence) on large pieces of paper leaving gaps labelled with the grammatical category (e.g. verb, noun) for the missing words. Write the word lists on index cards, labelled on the back with the grammatical category. Keep the cards in separate piles corresponding to their grammatical category.

To generate a love letter, use the following algorithm:

  1. Shuffle the template sheets and lay them out in a random order (apart from salutation and sign off ones which go at start and end).
  2. Repeat the following until all gaps are filled:
    1. Take the appropriate pile of cards  for a word gap and shuffle them.
    2. Take a word at random from the pile.
    3. Place it in the space.

Evaluate your templates and word lists thoroughly be doing this repeatedly, improving it as needed. A variation of this kind of paper and card prototyping is a common way professionals do early prototyping and user testing of algorithms and interfaces, to get them right before too much time and effort is put into creating the actual implementation.

You can alternatively just use a simple template for the whole thing and write selected words in the boxes. Here is one I created, with some sample word lists.

For groups that can program, have them now implement their love letter generator in a programming language of their choice. It is a really good introductory exercise that can be developed iteratively getting better and better. Get each version to work and then improve it to do something more or what it does better. Examples include:

  • Start by writing a program that just asks the user to draw random unplugged cards from the right category and enter the word chosen.
  • Then introduce a random method and use if statements to then print the word corresponding to the random number.
  • Next have separate methods for each template line
  • Then introduce arrays or lists for the word lists in place of the if statements
  • Add more templates
  • Allow the template methods to be chosen at random (possibly multiple times)
  • Make sure each template line is used at most once
  • and so on …

The unplugged version of this activity does not just have to be seen as play. Creating such prototypes to test ideas is an important design phase, that professionals would sensibly do to test ideas before coding.

You can also set English puzzles based on working out what the templates were that generated given messages. Here is an example:

Activity: Christmas Greeting writer

“Mrs Ashman ‏@GH_CompSci I had a similar idea and was going to use the Mr Bean clip where he is sending Xmas cards to himself as a starter”

You can do the above activity with other kinds of letter. For example at Christmas why not do the activity to generate Christmas card messages. You will never have to write a Christmas card again. Here is an example from my first attempt:

Season’s Greetings!

    I wish you a wonderful holiday.

    I hope to catch up with you in the new year.

with love Q

I used the templates:

“I wish you a ” + adjective + ” ” + noun + “.”

“I hope ” + verb + ” you ” + adverb + “.”

My word lists were:

salutations = {“Happy Christmas”, “Merry Christmas”, “Season’s Greetings”, “Happy New Year”}
adjectives = {“wonderful”, “joyous”, “peaceful”, “relaxing”}
nouns = {“holiday”, “time”, “few weeks”, “christmas”}
verbs = {“to see”, “to catch up with”, “to be with”, “to snog”}
adverbs = {“soon”, “in the new year”, “sometime soon”, “before long”}
endings = {“with love”, “best wishes”, “all my love”}

Here is a slightly later version of my algorithm implemented as a program in Python

Either try to write it yourself from scratch, or modify it with different word lists and new sentence templates.

Can your class do better? Perhaps they can create a Christmas love letter writing algorithm.

There are lots of variations (perhaps let the students decide)

  • Activity: Postcard writer
    • Do the same for other kinds of writing. For example, why not create one that writes postcards for you so you don’t have to interrupt reading that thriller by the poolside.
  • Activity: Lyric rewriter
    • Take your favourite song lyrics and write a program that rewrites them at random, eg “Ain’t no sunshine when she’s gone … might become “Ain’t no cabbage when she’s snoring …”
  • Activity: Halloween message  generator
    • Create a program / unplugged generator that generates spooky Happy halloween messages. “Have a haunting time”, ” Have a spooky time” … “May your night be filled with Vampires” etc
  • Activity: Insult generator
    • At the end of the Batman Lego movie, Batman and Joker trade insults about how they are each other’s true nemesis and will hate each other forever. In this spirit, write a ridiculous insult generator. This should be combined with a lesson on ethics.
  • Activity: Counting story generator
    • Have the program generate counting stories “10 happy bananas sitting on a cake, …
  • Activity: Poem writer
    • Can you adapt the idea to write poems. If you want them to rhyme you need lists of rhyming words.