User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
DaniWeb is a massive community of 375,276 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,047 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Mar 13th, 2007
Views: 1,892
wordify:
Converts any integer number (such as 123456000999) into a string ("one billion two hundred ninety-three million one hundred two thousand one hundred").
haskell Syntax | 1 stars
  1. module Wordify (wordifyWords, wordify) where
  2.  
  3. digiteenNames :: [String]
  4. digiteenNames = ["", "one", "two", "three", "four", "five"
  5. ,"six", "seven", "eight", "nine", "ten"
  6. ,"eleven", "twelve", "thirteen", "fourteen"
  7. ,"fifteen","sixteen", "seventeen"
  8. ,"eighteen", "nineteen"
  9. ]
  10.  
  11. -- Returns the string form of a particular digit.
  12. digiteen :: Integer -> String
  13. digiteen n = digiteenNames !! fromIntegral n
  14.  
  15. tenNames :: [String]
  16. tenNames = ["","","twenty", "thirty", "forty", "fifty"
  17. ,"sixty", "seventy", "eighty", "ninety"
  18. ]
  19.  
  20. -- Returns the string form for a particular multiple of ten.
  21. tenName :: Integer -> String
  22. tenName n = tenNames !! fromIntegral n
  23.  
  24. -- Converts a two digit number to its string form.
  25. twodigit :: Integer -> String
  26. twodigit n | n < 20 = digiteen n
  27. | units == 0 = tenName tens
  28. | otherwise = tenName tens ++ "-" ++ digiteen units
  29. where (tens, units) = divMod n 10
  30.  
  31. -- Converts a three digit number to an array of words. For example:
  32. --
  33. -- threedigit 54 = ["fifty-four"]
  34. -- threedigit 923 = ["nine", "hundred", "twenty-three"]
  35. -- threedigit 400 = ["four", "hundred"]
  36.  
  37. threedigit :: Integer -> [String]
  38. threedigit n
  39. | hundi == 0 = [twodigit n]
  40. | twodi == 0 = [digiteen hundi, "hundred"]
  41. | otherwise = [digiteen hundi, "hundred", twodigit twodi]
  42. where (hundi, twodi) = divMod n 100
  43.  
  44.  
  45. -- periodNames = [[], ["thousand"], ["million"], ...
  46. --
  47. -- This list uses fake names after decillion.
  48. periodNames :: [[String]]
  49. periodNames = [] : map (:[]) ps
  50. where ps = ["thousand", "million", "billion", "trillion"
  51. ,"quadrillion", "quintillion", "sextillion"
  52. ,"octillion", "septillion", "novemtillion"
  53. ,"decillion"] ++ fakes 11
  54. fakes n = (wordify n ++ "-tillion") : fakes (n + 1)
  55.  
  56. -- Converts a large (or small) positive number to a list of words.
  57. manydigit :: Integer -> [String]
  58. manydigit m = aux periodNames m []
  59. where aux _ 0 tail = tail
  60. aux (p:ps) n tail
  61. = let (n', front) = divMod n 1000
  62. in aux ps n' (case front of
  63. 0 -> tail
  64. _ -> threedigit front ++ p ++ tail)
  65.  
  66. -- Converts any integer to word form, making a list of words.
  67. wordifyWords :: Integer -> [String]
  68. wordifyWords n
  69. | n < 0 = "negative" : manydigit (negate n)
  70. | n == 0 = ["zero"] -- a special case!
  71. | otherwise = manydigit n
  72.  
  73. -- Converts an integer to string form. For example,
  74. -- wordify 123456000999 = "one hundred twenty-three billion \
  75. -- \four hundred fifty-six million \
  76. -- \nine hundred ninety-nine"
  77. wordify :: Integer -> String
  78. wordify = unwords . wordifyWords
Comments (Newest First)
gwern | Newbie Poster | Apr 10th, 2008
Lardmeister: a couple reasons. First, every GHC executable comes with the RTS, runtime system. This handles garbage collection, threading (or lack there of), profiling, and so on. There's actually quite a few options for it. This RTS eats up a couple hundred KB.
Second, the executables are statically linked - there is no libh along the lines of libc. Dynamic/shared libraries are coming; I'm not sure when, but I heard the GHC devs got it working on some non-Linux platform already, so my guess would be 6.10.
And third, I believe GHC by default leaves all the debugging symbols and whatnot in. Running strip might reclaim some space.

Finally, I'd like to note that binary size isn't so terrible since a lot of the code size is a fixed cost as I mentioned - it looks worst for a hello world. My customized XMonad, when stripped, is about 2.4 megabytes, while your hello world example would lead one to suspect a size considerably worse... Also, come on, this is an old and uninteresting topic. Does it bother you when you run a Python or Java or Common Lisp app that the interpreter is like 20-60 megs?
Rashakil Fol | Salamander Man | Apr 9th, 2007
I don't know. Because there's a bit of work and code that has to be executed for the garbage collector and other systems, I guess.
Lardmeister | Veteran Poster | Apr 6th, 2007
Why does Haskell give such huge executable files? I compiled a simple "Hello world!" program using 'ghc --make hello.hs' and came up with an 'hello.exe' file of over 700 kb!
Rashakil Fol | Salamander Man | Mar 13th, 2007
To evaluate the function, run ghci, load the file Wordify.hs with :l Wordify, and type wordify 123456000999 at the prompt (or some other argument).
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 6:15 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC