Created
July 2, 2019 13:54
-
-
Save bitwombat/2378f1932137993504d640487f46686b to your computer and use it in GitHub Desktop.
Exercise from page 294 of Haskell Book - Numbers into words
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | |
digitToWord _ = "nope" | |
digits :: Int -> [Int] | |
digits num = go num [] | |
where | |
go n lst | |
| q == 0 = [r] ++ lst | |
| otherwise = go q $ [r] ++ lst | |
where q = div n 10 | |
r = mod n 10 | |
digitsToWords :: [Int] -> [String] | |
digitsToWords ds = map digitToWord ds | |
wordsToString :: [String] -> String | |
wordsToString ws = concat $ intersperse "-" ws | |
say :: Int -> String | |
say = wordsToString . digitsToWords . digits |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment