Skip to content

Food Chain

Recite the cumulative song about the old lady who swallowed a fly, where each verse swallows a bigger animal to catch the one before.

Adapted from Exercism’s problem specification (MIT). Listed under Strings & Text.

// FILE: food_chain.nomi
//# The cumulative song "I Know an Old Lady Who Swallowed a Fly".

/// An animal the old lady swallows: its name, the line sung about it when
/// she swallows it, and what follows its name when it is the one caught.
struct Animal
    name:     String
    reaction: Maybe<String> = None
    detail:   String = ""
end

/// The animals in the order she swallows them. Nothing is said about the fly:
/// its verse goes straight to the closing line.
once animals = [
    Animal{name: "fly"},
    {
        name:     "spider",
        reaction: Some("It wriggled and jiggled and tickled inside her."),
        detail:   " that wriggled and jiggled and tickled inside her",
    },
    {name: "bird", reaction: Some("How absurd to swallow a bird!")},
    {name: "cat", reaction: Some("Imagine that, to swallow a cat!")},
    {name: "dog", reaction: Some("What a hog, to swallow a dog!")},
    {name: "goat", reaction: Some("Just opened her throat and swallowed a goat!")},
    {name: "cow", reaction: Some("I don't know how she swallowed a cow!")},
]

/// The last verse, after the cow: she swallows a horse and dies, so nothing
/// is caught.
once horse_verse = ["I know an old lady who swallowed a horse.", "She's dead, of course!"]

once closing = "I don't know why she swallowed the fly. Perhaps she'll die."

/// The lines of verses `start` to `end`, both included, counting from 1, with
/// an empty line between two verses.
pub fn recite(start: Int, stop: Int) -> List<String>
    start..=stop
    |> Iter.map(verse)
    |> Iter.intersperse([""])
    |> Iter.flatten()
    |> Iter.to_list()
end

/// Verse `number`: the new animal, what she said about it, and every animal
/// caught back down to the fly.
fn verse(number: Int) -> List<String>
    if number > Iter.count(animals) then return horse_verse

    swallowed = animals |> Iter.take(number) |> Iter.reverse()
    [newest, ..] = swallowed else unreachable "every verse up to the cow swallows at least the fly"

    chain =
        swallowed
        |> Iter.zip(Iter.drop(swallowed, 1))
        |> Iter.map(fn (predator, prey) ->
            "She swallowed the ${predator.name} to catch the ${prey.name}${prey.detail}."
        end)

    ["I know an old lady who swallowed a ${newest.name}.", ..newest.reaction, ..chain, closing]
end

// FILE: food_chain_test.nomi
import food_chain

test "food chain"
    [
        (
            "fly",
            1,
            1,
            [
                "I know an old lady who swallowed a fly.",
                "I don't know why she swallowed the fly. Perhaps she'll die.",
            ],
        ),
        (
            "spider",
            2,
            2,
            [
                "I know an old lady who swallowed a spider.",
                "It wriggled and jiggled and tickled inside her.",
                "She swallowed the spider to catch the fly.",
                "I don't know why she swallowed the fly. Perhaps she'll die.",
            ],
        ),
        (
            "bird",
            3,
            3,
            [
                "I know an old lady who swallowed a bird.",
                "How absurd to swallow a bird!",
                "She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.",
                "She swallowed the spider to catch the fly.",
                "I don't know why she swallowed the fly. Perhaps she'll die.",
            ],
        ),
        (
            "cat",
            4,
            4,
            [
                "I know an old lady who swallowed a cat.",
                "Imagine that, to swallow a cat!",
                "She swallowed the cat to catch the bird.",
                "She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.",
                "She swallowed the spider to catch the fly.",
                "I don't know why she swallowed the fly. Perhaps she'll die.",
            ],
        ),
        (
            "dog",
            5,
            5,
            [
                "I know an old lady who swallowed a dog.",
                "What a hog, to swallow a dog!",
                "She swallowed the dog to catch the cat.",
                "She swallowed the cat to catch the bird.",
                "She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.",
                "She swallowed the spider to catch the fly.",
                "I don't know why she swallowed the fly. Perhaps she'll die.",
            ],
        ),
        (
            "goat",
            6,
            6,
            [
                "I know an old lady who swallowed a goat.",
                "Just opened her throat and swallowed a goat!",
                "She swallowed the goat to catch the dog.",
                "She swallowed the dog to catch the cat.",
                "She swallowed the cat to catch the bird.",
                "She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.",
                "She swallowed the spider to catch the fly.",
                "I don't know why she swallowed the fly. Perhaps she'll die.",
            ],
        ),
        (
            "cow",
            7,
            7,
            [
                "I know an old lady who swallowed a cow.",
                "I don't know how she swallowed a cow!",
                "She swallowed the cow to catch the goat.",
                "She swallowed the goat to catch the dog.",
                "She swallowed the dog to catch the cat.",
                "She swallowed the cat to catch the bird.",
                "She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.",
                "She swallowed the spider to catch the fly.",
                "I don't know why she swallowed the fly. Perhaps she'll die.",
            ],
        ),
        (
            "horse",
            8,
            8,
            [
                "I know an old lady who swallowed a horse.",
                "She's dead, of course!",
            ],
        ),
        (
            "multiple verses",
            1,
            3,
            [
                "I know an old lady who swallowed a fly.",
                "I don't know why she swallowed the fly. Perhaps she'll die.",
                "",
                "I know an old lady who swallowed a spider.",
                "It wriggled and jiggled and tickled inside her.",
                "She swallowed the spider to catch the fly.",
                "I don't know why she swallowed the fly. Perhaps she'll die.",
                "",
                "I know an old lady who swallowed a bird.",
                "How absurd to swallow a bird!",
                "She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.",
                "She swallowed the spider to catch the fly.",
                "I don't know why she swallowed the fly. Perhaps she'll die.",
            ],
        ),
        (
            "full song",
            1,
            8,
            [
                "I know an old lady who swallowed a fly.",
                "I don't know why she swallowed the fly. Perhaps she'll die.",
                "",
                "I know an old lady who swallowed a spider.",
                "It wriggled and jiggled and tickled inside her.",
                "She swallowed the spider to catch the fly.",
                "I don't know why she swallowed the fly. Perhaps she'll die.",
                "",
                "I know an old lady who swallowed a bird.",
                "How absurd to swallow a bird!",
                "She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.",
                "She swallowed the spider to catch the fly.",
                "I don't know why she swallowed the fly. Perhaps she'll die.",
                "",
                "I know an old lady who swallowed a cat.",
                "Imagine that, to swallow a cat!",
                "She swallowed the cat to catch the bird.",
                "She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.",
                "She swallowed the spider to catch the fly.",
                "I don't know why she swallowed the fly. Perhaps she'll die.",
                "",
                "I know an old lady who swallowed a dog.",
                "What a hog, to swallow a dog!",
                "She swallowed the dog to catch the cat.",
                "She swallowed the cat to catch the bird.",
                "She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.",
                "She swallowed the spider to catch the fly.",
                "I don't know why she swallowed the fly. Perhaps she'll die.",
                "",
                "I know an old lady who swallowed a goat.",
                "Just opened her throat and swallowed a goat!",
                "She swallowed the goat to catch the dog.",
                "She swallowed the dog to catch the cat.",
                "She swallowed the cat to catch the bird.",
                "She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.",
                "She swallowed the spider to catch the fly.",
                "I don't know why she swallowed the fly. Perhaps she'll die.",
                "",
                "I know an old lady who swallowed a cow.",
                "I don't know how she swallowed a cow!",
                "She swallowed the cow to catch the goat.",
                "She swallowed the goat to catch the dog.",
                "She swallowed the dog to catch the cat.",
                "She swallowed the cat to catch the bird.",
                "She swallowed the bird to catch the spider that wriggled and jiggled and tickled inside her.",
                "She swallowed the spider to catch the fly.",
                "I don't know why she swallowed the fly. Perhaps she'll die.",
                "",
                "I know an old lady who swallowed a horse.",
                "She's dead, of course!",
            ],
        ),
    ]
    |> subtests (description, start, stop, expected), description
        assert food_chain.recite(start, stop) == expected
    end
end
  • The song is data: a list of Animal structs with a name, the line sung when she swallows it, and a detail that follows its name when it is the one caught. Only the spider has a detail (“that wriggled and jiggled and tickled inside her”), so the field defaults to "", and the fly, about which nothing is sung, leaves its reaction at its default, None.
  • A verse takes the animals swallowed so far, newest first. The chain of “She swallowed the X to catch the Y” lines zips that list with itself shifted by one, so each line pairs a predator with its prey and no line is built by index.
  • A list literal’s spread takes any Iter of its elements, and a Maybe is an Iter of zero or one, so ..newest.reaction splices the reaction’s line, or no line for the fly, and ..chain splices the lazy chain of lines with no Iter.to_list.
  • The horse is not an Animal: its verse is the opening and “She’s dead, of course!”, with no chain and no closing line, so it is a fixed horse_verse that verse answers for any number past the cow.
  • The newest animal is taken with a list pattern, [newest, ..] = swallowed, whose else is unreachable, since every verse up to the cow swallows the fly at least (style §29).
  • recite answers lines with an empty line between two verses, as bottle-song does.
  • The ten canonical cases are one subtests table of the canonical lines written out.