Skip to content

Instantly share code, notes, and snippets.

@Ummon
Created November 10, 2017 21:23
Show Gist options
  • Save Ummon/7cffb6264249ede0b77f93e98163c089 to your computer and use it in GitHub Desktop.
Save Ummon/7cffb6264249ede0b77f93e98163c089 to your computer and use it in GitHub Desktop.
Transform an integer to a roman numeral
#!/usr/bin/env fsharpi
let elements = [ "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 ]
let rec toRomanNumber (v : int) : string =
if v >= 1 then
let e, v' = elements |> List.find (snd >> (>=) v)
e + toRomanNumber (v - v')
else
""
for i = 1 to 100 do
printfn "%i: %s" i (toRomanNumber i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment