Roman numerals/Encode: Difference between revisions
→{{header|Haskell}}: Added type signatures to first version, and applied hlint, hindent
m (→{{header|Haskell}}: adjusted concats) |
(→{{header|Haskell}}: Added type signatures to first version, and applied hlint, hindent) |
||
Line 1,926:
With an explicit decimal digit representation list:
<lang haskell>digit
digit x y z k =
[[x], [x, x], [x, x, x], [x, y], [y], [y, x], [y, x, x], [y, x, x, x], [x, z]] !!
(fromInteger k - 1)
toRoman :: Integer -> String
toRoman 0 = ""
toRoman x
toRoman x
toRoman x | x >= 100 = digit 'C' 'D' 'M' q ++ toRoman r where ▼
| x >= 1000 = 'M' : toRoman (x - 1000)
(q,r) = x `divMod` 100▼
toRoman x
toRoman x | x >= 10 = digit 'X' 'L' 'C' q ++ toRoman r where ▼
(q,r) = x `divMod` 10▼
where
toRoman x = digit 'I' 'V' 'X' x</lang>▼
▲ (q, r) = x `divMod` 100
toRoman x
where
▲ (q, r) = x `divMod` 10
main :: IO ()
{{out}}
<pre>["MCMXCIX","XXV","CMXLIV"]</pre>▼
▲<pre>*Main> map toRoman [1999,25,944]
▲["MCMXCIX","XXV","CMXLIV"]</pre>
or, defining '''roman''' in terms of mapAccumL
|