Skip to content

Say

Spell out a number from zero to 999,999,999,999 in English words.

Adapted from Exercism’s problem specification (MIT). Listed under Numbers & Math.

// FILE: say.nomi
//# Spells out a number in English.

/// Why a number cannot be said.
pub enum Error
    OutOfRange
end

/// The largest number `say` spells.
once largest = 999_999_999_999

/// The words for 0 to 19.
once small = Vector[
    "zero",
    "one",
    "two",
    "three",
    "four",
    "five",
    "six",
    "seven",
    "eight",
    "nine",
    "ten",
    "eleven",
    "twelve",
    "thirteen",
    "fourteen",
    "fifteen",
    "sixteen",
    "seventeen",
    "eighteen",
    "nineteen",
]

/// The words for each multiple of ten, by its tens digit; 0 and 1 have none.
once tens = Vector[
    "",
    "",
    "twenty",
    "thirty",
    "forty",
    "fifty",
    "sixty",
    "seventy",
    "eighty",
    "ninety",
]

/// Each scale word with the number it counts, largest first.
once scales: List<(PositiveInt, String)> = [
    (1_000_000_000, "billion"),
    (1_000_000,     "million"),
    (1000,          "thousand"),
    (100,           "hundred"),
]

/// `number` in English words, from "zero" to the largest number below a
/// trillion.
pub fn say(number: Int) -> Result<String, Error>
    if not number in 0..=largest then
        Err(.OutOfRange)
    else
        Ok(words(number))
    end
end

/// `n` in words: the largest scale it reaches, said as a count of that
/// scale and then the rest, or below a hundred the words for its tens and
/// ones.
fn words(n: Int) -> String
    case Iter.find(scales, fn (size, _name) -> n >= size)
        Some((size, name)) ->
            head = "${words(n / size)} ${name}"

            if n % size == 0 then
                head
            else
                "${head} ${words(n % size)}"
            end

        None ->
            below_hundred(n)
    end
end

fn below_hundred(n: Int) -> String
    case
        n < 20      -> Vector.get(small, n)!
        n % 10 == 0 -> Vector.get(tens, n / 10)!
        _           -> "${Vector.get(tens, n / 10)!}-${Vector.get(small, n % 10)!}"
    end
end

// FILE: say_test.nomi
import say.{self, Error}

test "say"
    [
        ("zero", 0, Ok("zero")),
        ("one", 1, Ok("one")),
        ("fourteen", 14, Ok("fourteen")),
        ("twenty", 20, Ok("twenty")),
        ("twenty-two", 22, Ok("twenty-two")),
        ("thirty", 30, Ok("thirty")),
        ("ninety-nine", 99, Ok("ninety-nine")),
        ("one hundred", 100, Ok("one hundred")),
        ("one hundred twenty-three", 123, Ok("one hundred twenty-three")),
        ("two hundred", 200, Ok("two hundred")),
        ("nine hundred ninety-nine", 999, Ok("nine hundred ninety-nine")),
        ("one thousand", 1000, Ok("one thousand")),
        ("one thousand two hundred thirty-four", 1234, Ok("one thousand two hundred thirty-four")),
        ("one million", 1_000_000, Ok("one million")),
        (
            "one million two thousand three hundred forty-five",
            1_002_345,
            Ok("one million two thousand three hundred forty-five"),
        ),
        ("one billion", 1_000_000_000, Ok("one billion")),
        (
            "a big number",
            987_654_321_123,
            Ok(
                "nine hundred eighty-seven billion six hundred fifty-four million three hundred twenty-one thousand one hundred twenty-three"
            ),
        ),
        ("numbers below zero are out of range", -1, Err(Error.OutOfRange)),
        ("numbers above 999,999,999,999 are out of range", 1_000_000_000_000, Err(.OutOfRange)),
    ]
    |> subtests (description, number, expected), description
        assert say.say(number) == expected
    end
end
  • say returns Result<String, Error>, with Error.OutOfRange for a number below zero or above 999,999,999,999. The range check is the one place a number can fail, so the rest of the code works on numbers already in range.
  • words is one recursive rule for every scale: find the largest of billion, million, thousand and hundred that the number reaches, say how many of it (words(n / size)), then say the rest when there is one. The scales are a once table annotated List<(PositiveInt, String)>, so each size is a divisor / and % take. “Hundred” is a scale like the others, so “nine hundred eighty-seven billion” falls out of the same rule twice.
  • Below a hundred, the words come from two Vector tables, small for 0 to 19 and tens by tens digit, joined with a hyphen when the ones digit is not zero. The tables are once bindings, read with Vector.get(...)! at an index the range check and the arithmetic keep in bounds (style §29).
  • The scales are a list of (size, name) tuples, largest first, so Iter.find with n >= size picks the largest one the number reaches.
  • Large literals are written with digit separators, 987_654_321_123, in the solution and the tests.
  • The 19 canonical cases are one subtests table named by the description column; the out-of-range cases are rows whose expected value is an Err.