gdritter repos documents / master posts / madlibs-intro.md
master

Tree @master (Download .tar.gz)

madlibs-intro.md @masterview rendered · raw · history · blame

Years ago, I worked at the [Berkeley Self-Paced Center], which is a
wonderful institution. It was designed to be a self-directed,
one-on-one center where people could go to learn a new programming
language (or sometimes programming environment) in a structured way
but at the student's own pace[^1].

[^1]: At least, nominally; in practice, the large influx of students
at the end of the semester—who had forgotten about their enrollment
until on the verge of failing—led us to experiment with various
ways of imposing a loose schedule on students, so that
we could try to even out the frequency of student visits.

One of the early assignments in these courses I really liked,
because it was (I felt) a program that was not particularly complicated
or large, but at the same time conveyed several useful,
_practical_ concepts that don't show up in Hello World or Project Euler
examples.

The example was a simple MadLibs program. MadLibs is a simple game in
which one is presented first with a series of blanks accompanied by
requests for particular classes of words—sometimes parts of speech
such as nouns or verbs, sometimes more specific descriptors such as
food items or animals—and then those are stitched into a sentence,
where they produce humorously nonsensical phrases.[^2]

The program in question required that the MadLib be chosen randomly
from a list of possible ones, and that the blanks be requested in
a random order which did not necessarily correspond to their ordering
in the template phrase.

A program to implement this would necessarily involve several tasks
in the relevant language: input _and_ output from a user, randomness,
some kind of data structure for keeping the intermediate values, and
possibly some kind of string processing. If one adds a small extra
stipulation—that the MadLibs templates must be read from a file—then
it also involves file IO, making it a good whirlwind tour of a
language's features in practice.

What I'd like to do is use this MadLibs program as a way of exploring
other programming languages, and in particular, niche, emerging, or
forgotten languages which people might not be widely familiar with.

## The MadLibs Program

The rough steps which will be performed in most versions of the
program are as follows:

1. Open a file named `template.mad`. This file contains one or more
lines that have sentences with certain parts inside matching
curly braces, e.g.

> This {noun} will {verb}!

After this file is read in, one of the lines is selected at random
to be the template used.

2. Split that template into two sequences: the literal text segments
and the part-of-speech segments, as in

> ["This",         "will",         "!"]
> [        "noun",         "verb"     ]

3. Print out requests for the part-of-speech segments, read in
lines from the user, and fill them into the template. Do this in
random order but still filling in the correct gap.

> Give me a verb: jump
> Give me a noun: dog

4. Print the resulting sentence

> This dog will jump!

[^2]: I played this a lot as a child, in which most of the words
chosen were scatological or otherwise puerile. Which is to say:
most of the blanks were filled in with some variation on "poop".