martes, 2 de septiembre de 2014

Decimal to Romans - Haskell Style

As promised...here's my Haskell take on Decimal to Romans...I got say...it took me considerably less time to build it Haskell that it took me to build it Erlang...but for sure...I had already done it in Erlang...so I had some sort of advantage -;)

Anyway...this was so much fun to do...and couldn't be happier with the overall process...Haskell being so pure is really a joy to work with...

Too much talk...here's the source code...

Roman_Numerals.hs
showRomans :: Int -> IO()
showRomans(num) = do
 putStr $ concat $ get_roman num 0

get_roman :: Int -> Int -> [[Char]]
get_roman num ctr
 | num >= roman = make_roman(roman) ++ get_roman(num - roman) ctr
 | num < roman && num > 0 = get_roman(num) (ctr+1)
 | num <= 0 = ["\n"]
 where roman = roman_keys [] !! ctr

make_roman :: Int -> [[Char]]
make_roman(1) = ["I"]; make_roman(4) = ["IV"]; make_roman(5) = ["V"];
make_roman(9) = ["IX"]; make_roman(10) = ["X"]; make_roman(40) = ["XL"];
make_roman(50) = ["L"]; make_roman(90) = ["XC"]; make_roman(100) = ["C"];
make_roman(400) = ["CD"]; make_roman(500) = ["D"]; make_roman(900) = ["CM"];
make_roman(1000) = ["M"]

roman_keys :: [Int] -> [Int]
roman_keys keys = [1000,900,500,400,100,90,50,40,10,9,5,4,1]

As usual...here's the screenshot...


After so many blogs in just a few days...I think I deserve a break, so I can keep reading the Haskell book...as I promised a review of it...

Greetings,

Blag.
Development Culture.

No hay comentarios: