Roman numerals/Decode: Difference between revisions
→Haskell :: mapAccum: Preferred Data.Bifunctor to Control.Arrow, applied Ormolu
(→Haskell :: mapAccum: Preferred Data.Bifunctor to Control.Arrow, applied Ormolu) |
|||
Line 3,046:
====mapAccum====
Or, expressing '''romanValue''' in terms of '''mapAccumL''' (avoiding recursive descent, and visiting each k v pair just once)
<lang Haskell>import Data.
import
romanValue :: String -> Int
romanValue =
let tr s (k, v) =
until
in sum .▼
(bimap ((drop . length) k) (v +))
snd .▼
("IV", 4),
("I", 1)
main :: IO ()
main =
mapM_
(print . romanValue)
[ "MDCLXVI",
"MCMXC",
"MMVIII",
"MMXVI",
"MMXVII"
]</lang>
Or, in a '''mapAccumR''' version:
|