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 x:: yChar z-> kChar =-> Char -> Integer -> String
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 | x < 0 = error "Negative roman numeral"
toRoman x | x >=< 10000 = 'M'error : toRoman (x"Negative -roman 1000)numeral"
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
toRoman x | x >= 100 = digit 'C' 'D' 'M' 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
toRoman x | x >= 10 = digit 'X' 'L' 'C' q ++ toRoman r where
where
(q, r) = x `divMod` 10
toRoman x = digit 'I' 'V' 'X' x</lang>
 
main :: IO ()
<pre>*Main>main map= print $ toRoman <$> [1999, 25, 944]</lang>
{{out}}
<pre>["MCMXCIX","XXV","CMXLIV"]</pre>
<pre>*Main> map toRoman [1999,25,944]
["MCMXCIX","XXV","CMXLIV"]</pre>
 
 
or, defining '''roman''' in terms of mapAccumL
9,655

edits