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

From HaskellWiki
Jump to navigation Jump to search
(No difference)

Revision as of 17:17, 15 July 2010

(**) 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.