RosettaCodeData/Task/Roman-numerals-Decode/Logo/roman-numerals-decode.logo
Ingy döt Net b83f433714 tasks a-s
2013-04-10 23:57:08 -07:00

35 lines
973 B
Text

; Roman numeral decoder
; First, some useful substring utilities
to starts_with? :string :prefix
if empty? :prefix [output "true]
if empty? :string [output "false]
if not equal? first :string first :prefix [output "false]
output starts_with? butfirst :string butfirst :prefix
end
to remove_prefix :string :prefix
if or empty? :prefix not starts_with? :string :prefix [output :string]
output remove_prefix butfirst :string butfirst :prefix
end
; Our list of Roman numeral values
make "values [[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]]
; Function to do the work
to from_roman :str
local "n make "n 0
foreach :values [
local "s make "s first ?
local "v make "v last ?
while [starts_with? :str :s] [
make "n sum n :v
make "str remove_prefix :str :s
]
]
output :n
end
foreach [MCMXC MDCLXVI MMVIII] [print (sentence (word ? "|: |) from_roman ?)]
bye