thoga31

num2text (PT)

Jan 23rd, 2014
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- For Portuguese language, only.
  2. -- It may not work with other languages.
  3.  
  4. import Data.Maybe (fromJust)
  5.  
  6. num2ext :: Integer -> String
  7. num2ext n | n <    0 || n >= maxlim = "ERRO"
  8.           | n >=   0 && n <= 9      = f (n,0)
  9.           | n >=  10 && n <  20     = f (n,1)
  10.           | n >=  20 && n < 100     = f (n `div`  10, 2) +:+ if n `mod`  10 == 0 then "" else num2ext (n `mod`  10)
  11.           | n == 100                = "cem"  -- caso especial
  12.           | n >  100 && n < 1000    = f (n `div` 100, 3) +:+ if n `mod` 100 == 0 then "" else num2ext (n `mod` 100)
  13.           | otherwise               = unwords $ map (\(x,y) -> if x `mod` 1000 == 0 then "" else num2ext x +:+ f (y, is1 x)) (reverse $ zip (reverse $ digs3 n) [0..])
  14.          
  15.     where
  16.         (+:+) [] [] = []
  17.         (+:+) xs [] = xs
  18.         (+:+) [] ys = ys
  19.         (+:+) xs ys = unwords [xs, ys]
  20.        
  21.         maxlim = (10^) . (*3) $ 5
  22.        
  23.         is1 1 = 5
  24.         is1 _ = 4
  25.        
  26.         digs3 0 = []
  27.         digs3 x = let d = x `div` 1000
  28.                       m = x `mod` 1000
  29.                   in digs3 d ++ m:[]
  30.        
  31.         countdigs = length . digits
  32.        
  33.         digits x | x >= 10   = (x `div` 10) : digits (x `mod` 10)
  34.                  | otherwise = [x]
  35.        
  36.         fst3 (x,_,_) = x
  37.         snd3 (_,x,_) = x
  38.         thr3 (_,_,x) = x
  39.        
  40.         f (c,0) = thr3 . fromJust $ lookup c conv100
  41.         f (c,1) =        fromJust $ lookup c conv10
  42.         f (c,2) = snd3 . fromJust $ lookup c conv100
  43.         f (c,3) = fst3 . fromJust $ lookup c conv100
  44.         f (c,4) = snd .  fromJust $ lookup c convplus
  45.         f (c,5) = fst .  fromJust $ lookup c convplus
  46.        
  47.         conv10 = zip [10..]
  48.                  ["dez",
  49.                   "onze",
  50.                   "doze",
  51.                   "treze",
  52.                   "catorze",
  53.                   "quinze",
  54.                   "dezasseis",
  55.                   "dezassete",
  56.                   "dezoito",
  57.                   "dezanove"]
  58.        
  59.         conv100 = zip [0..]
  60.                   [("",            "",         "zero"),
  61.                    ("cento",       "dez",      "um"),
  62.                    ("duzentos",    "vinte",    "dois"),
  63.                    ("trezentos",   "trinta",   "três"),
  64.                    ("quatrocentos","quarenta", "quatro"),
  65.                    ("quinhentos",  "cinquenta","cinco"),
  66.                    ("seiscentos",  "sessenta", "seis"),
  67.                    ("setecentos",  "setenta",  "sete"),
  68.                    ("oitocentos",  "oitenta",  "oito"),
  69.                    ("novecentos",  "noventa",  "nove")]
  70.        
  71.         convplus = zip [0..]
  72.                    [("",                ""),
  73.                     ("mil",             "mil"),
  74.                     ("milhão",          "milhões"),
  75.                     ("milhar de milhão","milhares de milhões"),
  76.                     ("bilião",          "biliões")]
Advertisement
Add Comment
Please, Sign In to add comment