Difference between revisions of "99 questions/Solutions/95"

From HaskellWiki
Jump to navigation Jump to search
 
(categorize)
 
Line 14: Line 14:
   
 
This solution does a simple table lookup after converting the positive integer into a string. Thus dividing into digits is much simplified.
 
This solution does a simple table lookup after converting the positive integer into a string. Thus dividing into digits is much simplified.
  +
  +
[[Category:Programming exercise spoilers]]

Latest revision as of 03:47, 10 January 2017

(**) English number words

On financial documents, like cheques, numbers must sometimes be written in full words. Example: 175 must be written as one-seven-five. Write a predicate full-words/1 to print (non-negative) integer numbers in full words.

import Data.Char
import Data.List

fullWords :: Integer -> String
fullWords n = concat $ intersperse "-" [digits!!digitToInt d | d <- show n]
  where digits = ["zero", "one", "two", "three", "four",
                  "five", "six", "seven", "eight", "nine"]

This solution does a simple table lookup after converting the positive integer into a string. Thus dividing into digits is much simplified.