 # paulfioravanti's solution

## to Roman Numerals in the Elm Track

Published at Aug 08 2019 · 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

## Elm Installation

Refer to the Installing Elm page for information about installing elm.

## Writing the Code

The first time you start an exercise, you'll need to ensure you have the appropriate dependencies installed. Thankfully, Elm makes that easy for you and will install dependencies when you try to run tests or build the code.

Execute the tests with:

``````\$ elm-test
``````

Automatically run tests again when you save changes:

``````\$ elm-test --watch
``````

As you work your way through the test suite, be sure to remove the `skip <|` calls from each test until you get them all passing!

## Source

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

## Submitting Incomplete Solutions

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

### Tests.elm

``````module Tests exposing (tests)

import Expect
import RomanNumerals exposing (toRoman)
import Test exposing (..)

tests : Test
tests =
describe "Roman Numerals"
[ test "1" <|
\() ->
Expect.equal "I"
(toRoman 1)
, skip <|
test "2" <|
\() ->
Expect.equal "II"
(toRoman 2)
, skip <|
test "3" <|
\() ->
Expect.equal "III"
(toRoman 3)
, skip <|
test "4" <|
\() ->
Expect.equal "IV"
(toRoman 4)
, skip <|
test "5" <|
\() ->
Expect.equal "V"
(toRoman 5)
, skip <|
test "6" <|
\() ->
Expect.equal "VI"
(toRoman 6)
, skip <|
test "9" <|
\() ->
Expect.equal "IX"
(toRoman 9)
, skip <|
test "27" <|
\() ->
Expect.equal "XXVII"
(toRoman 27)
, skip <|
test "48" <|
\() ->
Expect.equal "XLVIII"
(toRoman 48)
, skip <|
test "59" <|
\() ->
Expect.equal "LIX"
(toRoman 59)
, skip <|
test "93" <|
\() ->
Expect.equal "XCIII"
(toRoman 93)
, skip <|
test "141" <|
\() ->
Expect.equal "CXLI"
(toRoman 141)
, skip <|
test "163" <|
\() ->
Expect.equal "CLXIII"
(toRoman 163)
, skip <|
test "402" <|
\() ->
Expect.equal "CDII"
(toRoman 402)
, skip <|
test "575" <|
\() ->
Expect.equal "DLXXV"
(toRoman 575)
, skip <|
test "911" <|
\() ->
Expect.equal "CMXI"
(toRoman 911)
, skip <|
test "1024" <|
\() ->
Expect.equal "MXXIV"
(toRoman 1024)
, skip <|
test "3000" <|
\() ->
Expect.equal "MMM"
(toRoman 3000)
]``````
``````module RomanNumerals exposing (toRoman)

toRoman : Int -> String
toRoman number =
romanNumerals
|> List.foldl appendRomanNumeral ( "", number )
|> Tuple.first

-- PRIVATE

appendRomanNumeral : ( String, Int ) -> ( String, Int ) -> ( String, Int )
appendRomanNumeral ( roman, arabic ) ( numeral, number ) =
let
quotient =
number // arabic

remainder =
remainderBy arabic number

string =
numeral ++ String.repeat quotient roman
in
( string, remainder )

romanNumerals : List ( String, Int )
romanNumerals =
[ ( "M", 1000 )
, ( "CM", 900 )
, ( "D", 500 )
, ( "CD", 400 )
, ( "C", 100 )
, ( "XC", 90 )
, ( "L", 50 )
, ( "XL", 40 )
, ( "X", 10 )
, ( "IX", 9 )
, ( "V", 5 )
, ( "IV", 4 )
, ( "I", 1 )
]``````