Skip to content

Instantly share code, notes, and snippets.

@bitwombat
Created July 2, 2019 13:06
Show Gist options
  • Save bitwombat/ece15126c16eaf62e1bf0b1a3954c44d to your computer and use it in GitHub Desktop.
Save bitwombat/ece15126c16eaf62e1bf0b1a3954c44d to your computer and use it in GitHub Desktop.
Exercise from page 294 of Haskell Book - Numbers into words
module Page294 where
--Numbers into words
import Data.List (intersperse)
digitToWord :: Int -> String
digitToWord 0 = "zero"
digitToWord 1 = "one"
digitToWord 2 = "two"
digitToWord 3 = "three"
digitToWord 4 = "four"
digitToWord 5 = "five"
digitToWord 6 = "six"
digitToWord 7 = "seven"
digitToWord 8 = "eight"
digitToWord 9 = "nine"
digits :: Int -> [Int]
digits n = go n []
where go n lst
| (div n 10) == 0 = reverse $ lst ++ [(mod n 10)]
| otherwise = go (div n 10) $ lst ++ [(mod n 10)]
digitsToWords :: [Int] -> [String]
digitsToWords ds = map digitToWord ds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment