Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2015
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. import Data.List
  2. import Data.Maybe
  3. import Data.Tuple
  4. import Data.Vector (fromList, (!?))
  5.  
  6. item k xs = maybeToList . (fromList xs !?) . subtract k
  7.  
  8. units = item 1
  9. [ "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"
  10. , "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen"
  11. , "Seventeen", "Eighteen", "Nineteen" ]
  12.  
  13. tens = item 2
  14. [ "Twenty", "Thirty", "Forty", "Fifty"
  15. , "Sixty", "Seventy", "Eighty", "Ninety" ]
  16.  
  17. infixr 5 <:
  18.  
  19. _ <: [] = []
  20. "" <: xs = xs
  21. x <: xs = x : xs
  22.  
  23. part s n = s <: units u ++ tens t ++ "Hundred" <: units h
  24. where
  25. (h, m) = n `divMod` 100
  26. (t, u) | m < 20 = (0, m)
  27. | otherwise = m `divMod` 10
  28.  
  29. whole 0 = "Zero"
  30. whole n = unwords . reverse . concat . zipWith part
  31. [ "", "Thousand", "Million", "Billion", "Trillion", "Quadrillion"
  32. , "Quintillion", "Sextillion", "Septillion", "Octillion", "Nonillion" ]
  33. $ unfoldr go n
  34.  
  35. go 0 = Nothing
  36. go n = Just . swap $ n `divMod` 1000
  37.  
  38. main = putStrLn $ whole 79685746352413
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement