[Haskell-cafe] Roman to Decimal Algorithms

Martijn van Steenbergen martijn at van.steenbergen.nl
Sun Jun 7 14:25:15 EDT 2009

Hi Andrew,

I haven't read your whole message but if shortness is your goal, here 
are some ideas:

Andrew Savige wrote:
> rtoa c = 10^(205558`mod`(ord c)`mod`7)`mod`9995
> I'm not suggesting that magic formulae are useful outside of golf and
> this second rtoa function, though shorter, is much less clear. I might
> add that this particular magic formula appears to be less useful in
> Haskell golf than the other languages because `mod` is five times
> longer than the % operator of the other languages. :)

Why not rename mod like so?

rtoa c=10^(205558%ord c%7)%9995

> Alternatively, you could write the rtoa function using an approach
> taken from the original HaskellWiki solution:
> rtoa = fromJust . flip lookup (zip "IVXLCDM" [1,5,10,50,100,500,1000])

You can write this as:

Just rtoa=flip lookup (zip "IVXLCDM" [1,5,10,50,100,500,1000])

Which is another few characters shorter. :-)



More information about the Haskell-Cafe mailing list