Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import Data.List
- import Data.Maybe
- import Data.Tuple
- import Data.Vector (fromList, (!?))
- item k xs = maybeToList . (fromList xs !?) . subtract k
- units = item 1
- [ "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"
- , "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen"
- , "Seventeen", "Eighteen", "Nineteen" ]
- tens = item 2
- [ "Twenty", "Thirty", "Forty", "Fifty"
- , "Sixty", "Seventy", "Eighty", "Ninety" ]
- infixr 5 <:
- _ <: [] = []
- "" <: xs = xs
- x <: xs = x : xs
- part s n = s <: units u ++ tens t ++ "Hundred" <: units h
- where
- (h, m) = n `divMod` 100
- (t, u) | m < 20 = (0, m)
- | otherwise = m `divMod` 10
- whole 0 = "Zero"
- whole n = unwords . reverse . concat . zipWith part
- [ "", "Thousand", "Million", "Billion", "Trillion", "Quadrillion"
- , "Quintillion", "Sextillion", "Septillion", "Octillion", "Nonillion" ]
- $ unfoldr go n
- go 0 = Nothing
- go n = Just . swap $ n `divMod` 1000
- main = putStrLn $ whole 79685746352413
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement