Avatar of marionebl

marionebl's solution

to Roman Numerals in the OCaml Track

Published at Apr 26 2019 · 0 comments
Test suite


This exercise has changed since this solution was written.

Write a function to convert from normal numbers to Roman Numerals.

The Romans were a clever bunch. They conquered most of Europe and ruled it for hundreds of years. They invented concrete and straight roads and even bikinis. One thing they never discovered though was the number zero. This made writing and dating extensive histories of their exploits slightly more challenging, but the system of numbers they came up with is still in use today. For example the BBC uses Roman numerals to date their programmes.

The Romans wrote numbers using letters - I, V, X, L, C, D, M. (notice these letters have lots of straight lines and are hence easy to hack into stone tablets).

 1  => I
10  => X
 7  => VII

There is no need to be able to convert numbers larger than about 3000. (The Romans themselves didn't tend to go any higher)

Wikipedia says: Modern Roman numerals ... are written by expressing each digit separately starting with the left most digit and skipping any digit with a value of zero.

To see this in practice, consider the example of 1990.

In Roman numerals 1990 is MCMXC:

1000=M 900=CM 90=XC

2008 is written as MMVIII:

2000=MM 8=VIII

See also: http://www.novaroma.org/via_romana/numbers.html

Getting Started

For installation and learning resources, refer to the exercism help page.


To work on the exercises, you will need Opam and Core. Consult opam website for instructions on how to install opam for your OS. Once opam is installed open a terminal window and run the following command to install core:

opam install core

To run the tests you will need OUnit. Install it using opam:

opam install ounit

Running Tests

A Makefile is provided with a default target to compile your solution and run the tests. At the command line, type:


Interactive Shell

utop is a command line program which allows you to run Ocaml code interactively. The easiest way to install it is via opam:

opam install utop

Consult utop for more detail.

Feedback, Issues, Pull Requests

The exercism/ocaml repository on GitHub is the home for all of the Ocaml exercises.

If you have feedback about an exercise, or want to help implementing a new one, head over there and create an issue. We'll do our best to help you!


The Roman Numeral Kata http://codingdojo.org/cgi-bin/index.pl?KataRomanNumerals

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.


(* Test/exercise version: "1.2.0" *)

open OUnit2
open Roman_numerals

let ae expected actual _ctx = assert_equal ~printer:(fun x -> x) expected actual

let tests = [
   "1 is a single I" >::
      ae "I" (to_roman 1);
   "2 is two I's" >::
      ae "II" (to_roman 2);
   "3 is three I's" >::
      ae "III" (to_roman 3);
   "4, being 5 - 1, is IV" >::
      ae "IV" (to_roman 4);
   "5 is a single V" >::
      ae "V" (to_roman 5);
   "6, being 5 + 1, is VI" >::
      ae "VI" (to_roman 6);
   "9, being 10 - 1, is IX" >::
      ae "IX" (to_roman 9);
   "20 is two X's" >::
      ae "XXVII" (to_roman 27);
   "48 is not 50 - 2 but rather 40 + 8" >::
      ae "XLVIII" (to_roman 48);
   "49 is not 40 + 5 + 4 but rather 50 - 10 + 10 - 1" >::
      ae "XLIX" (to_roman 49);
   "50 is a single L" >::
      ae "LIX" (to_roman 59);
   "90, being 100 - 10, is XC" >::
      ae "XCIII" (to_roman 93);
   "100 is a single C" >::
      ae "CXLI" (to_roman 141);
   "60, being 50 + 10, is LX" >::
      ae "CLXIII" (to_roman 163);
   "400, being 500 - 100, is CD" >::
      ae "CDII" (to_roman 402);
   "500 is a single D" >::
      ae "DLXXV" (to_roman 575);
   "900, being 1000 - 100, is CM" >::
      ae "CMXI" (to_roman 911);
   "1000 is a single M" >::
      ae "MXXIV" (to_roman 1024);
   "3000 is three M's" >::
      ae "MMM" (to_roman 3000);

let () =
    run_test_tt_main ("roman-numerals test" >::: tests)
open Base

let pairs =
    (1000, "M"); (900, "CM"); 
    (500, "D"); (400, "CD"); 
    (100, "C"); (90, "XC"); 
    (50, "L"); (40, "XL"); 
    (10, "X"); (9, "IX"); 
    (5, "V"); (4, "IV"); 
    (1, "I")

let off x = 
  List.find pairs ~f:(fun (y, _) -> x >= y)

let to_roman n = 
  let rec aux x l =
    match (off) x with
    | None -> l
    | Some((y, c)) -> aux (x - y) (c :: l)
  aux n [] |> List.rev |> String.concat ~sep:""

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?