Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- For Portuguese language, only.
- -- It may not work with other languages.
- import Data.Maybe (fromJust)
- num2ext :: Integer -> String
- num2ext n | n < 0 || n >= maxlim = "ERRO"
- | n >= 0 && n <= 9 = f (n,0)
- | n >= 10 && n < 20 = f (n,1)
- | n >= 20 && n < 100 = f (n `div` 10, 2) +:+ if n `mod` 10 == 0 then "" else num2ext (n `mod` 10)
- | n == 100 = "cem" -- caso especial
- | n > 100 && n < 1000 = f (n `div` 100, 3) +:+ if n `mod` 100 == 0 then "" else num2ext (n `mod` 100)
- | otherwise = unwords $ map (\(x,y) -> if x `mod` 1000 == 0 then "" else num2ext x +:+ f (y, is1 x)) (reverse $ zip (reverse $ digs3 n) [0..])
- where
- (+:+) [] [] = []
- (+:+) xs [] = xs
- (+:+) [] ys = ys
- (+:+) xs ys = unwords [xs, ys]
- maxlim = (10^) . (*3) $ 5
- is1 1 = 5
- is1 _ = 4
- digs3 0 = []
- digs3 x = let d = x `div` 1000
- m = x `mod` 1000
- in digs3 d ++ m:[]
- countdigs = length . digits
- digits x | x >= 10 = (x `div` 10) : digits (x `mod` 10)
- | otherwise = [x]
- fst3 (x,_,_) = x
- snd3 (_,x,_) = x
- thr3 (_,_,x) = x
- f (c,0) = thr3 . fromJust $ lookup c conv100
- f (c,1) = fromJust $ lookup c conv10
- f (c,2) = snd3 . fromJust $ lookup c conv100
- f (c,3) = fst3 . fromJust $ lookup c conv100
- f (c,4) = snd . fromJust $ lookup c convplus
- f (c,5) = fst . fromJust $ lookup c convplus
- conv10 = zip [10..]
- ["dez",
- "onze",
- "doze",
- "treze",
- "catorze",
- "quinze",
- "dezasseis",
- "dezassete",
- "dezoito",
- "dezanove"]
- conv100 = zip [0..]
- [("", "", "zero"),
- ("cento", "dez", "um"),
- ("duzentos", "vinte", "dois"),
- ("trezentos", "trinta", "três"),
- ("quatrocentos","quarenta", "quatro"),
- ("quinhentos", "cinquenta","cinco"),
- ("seiscentos", "sessenta", "seis"),
- ("setecentos", "setenta", "sete"),
- ("oitocentos", "oitenta", "oito"),
- ("novecentos", "noventa", "nove")]
- convplus = zip [0..]
- [("", ""),
- ("mil", "mil"),
- ("milhão", "milhões"),
- ("milhar de milhão","milhares de milhões"),
- ("bilião", "biliões")]
Advertisement
Add Comment
Please, Sign In to add comment