ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# hyphenrf's solution

## to Roman Numerals in the OCaml Track

Published at Jul 08 2020 · 0 comments
Instructions
Test suite
Solution

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

## Getting Started

1. For library documentation, follow Useful OCaml resources.

## Running Tests

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

``````make
``````

## Submitting Incomplete Solutions

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

## 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 or submit a PR. We welcome new contributors!

## Source

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

### test.ml

``````(* roman-numerals - 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)``````
``````let roman_table = [ (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 rec roman ?(s="") ?(rs=roman_table) n =
let (i, r)::rst = rs in
match n with
| 0             -> s
| n when n >= i -> roman ~s:(s^r) ~rs:rs (n-i)
| n             -> roman ~s:s ~rs:rst n

let to_roman n = roman n``````

### 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?