Generating numerals



next up previous
Next: Deciding an election Up: Example Sheet Previous: Example Sheet

Generating numerals

  1. Write a function digit: int -> string that maps integers in the range 0..35 to their numeric representation as a string. For example,
        digit  0 = "0"
        digit  9 = "9"
        digit 10 = "A"
        digit 35 = "Z"

    Note that every character has a unique implementation dependent code; the ML function ord: string -> int returns the code of the first character of a string, and the ML function chr: int -> string returns the character corresponding to a code.

    An ML programmer can assume that the codes for digits are contiguous, as are the codes for letters; for example,

        chr(ord("A") + 1) = "B"

  2. Write a function scale: int * int -> string, such that scale(b,n) is the numeric representation of n in base b. You will need the builtin function: ^: string * string -> string which concatenates two strings, and has infix status. For instance, "Alice"^"Cooper" evaluates to "AliceCooper".

  3. Write a function, words: int -> string, such that words n is the number n expressed in words; for example,
        words 1004 = "one thousand and four"



Andy Gordon
Tue Jan 31 14:41:57 GMT 1995