The Downfall of Imperative Programming

Caligo iteronvexor at gmail.com
Wed Apr 11 08:49:52 PDT 2012


The Haskell version of Romans, rubies and D:

Modified version of http://www.haskell.org/haskellwiki/Roman_numerals
so that it's compile time.


---------------->8---------------->8----------------
module Romans where

import Language.Haskell.TH
import Maybe


roman :: String -> ExpQ
roman s = return $ LitE (IntegerL (value s))
  where value = fromIntegral . fst
                . foldr (\p (t,s) -> if p >= s then (t+p,p) else (t-p,p)) (0,0)
                . map (fromJust . flip lookup (zip "IVXLCDM"
[1,5,10,50,100,500,1000]))
----------------8<----------------8<----------------


And actual usage:

---------------->8---------------->8----------------
{-# LANGUAGE TemplateHaskell #-}

import Romans

main = print $(roman "VIX")
----------------8<----------------8<----------------


I still like the D version though.


More information about the Digitalmars-d mailing list