Roman numerals/Decode: Difference between revisions
Content deleted Content added
→Haskell :: mapAccum: Preferred Data.Bifunctor to Control.Arrow, applied Ormolu |
m →mapAccumR: Qualified import of Data.Map.Strict, tidied |
||
Line 3,088:
Or, in a '''mapAccumR''' version:
<lang Haskell>import Data.List (mapAccumR)
import qualified Data.Map.Strict as M
import Data.Maybe (maybe)
fromRoman :: String -> Maybe Int
fromRoman cs =
let go l r
| l > r = (- r, l)
| otherwise = (r, l)
in traverse (`M.lookup` mapRoman) cs
>>= ( Just . sum . ((:) <$> fst <*> snd
. mapAccumR go 0
)
mapRoman :: Map Char Int▼
▲mapRoman :: M.Map Char Int
mapRoman =
-- TEST ---------------------------------------------------▼
M.fromList $
zip
"MDCLXVI "
[ 1000,
500,
100,
50,
10,
5,
1,
0
]
main :: IO ()
main =
putStrLn $
fTable
"Decoding Roman numbers:\n"
show
(maybe "Unrecognised character" show)
fromRoman
[ "MDCLXVI",
"MCMXC",
"MMVIII",
-- FORMATTING ---------------------------------------------▼
"MMXVI",
"MMXVIII",
"MMXBIII"
]
fTable ::
String ->
(a -> String) ->
(b -> String) ->
(a -> b) ->
[a] ->
String
fTable s xShow fxShow f xs =
unlines $
let w = maximum (length . xShow <$> xs)▼
s :
rjust n c = drop <$> length <*> (replicate n c ++)▼
<*> ((" -> " <>) . fxShow . f)
)
xs
where
{{Out}}
<pre>Decoding Roman numbers:
|