Skip to content

Instantly share code, notes, and snippets.

@lzakharov
Created November 10, 2019 23:49
Show Gist options
  • Select an option

  • Save lzakharov/309a1343e06d1797c5fece8bcd179a02 to your computer and use it in GitHub Desktop.

Select an option

Save lzakharov/309a1343e06d1797c5fece8bcd179a02 to your computer and use it in GitHub Desktop.
wordNumber returns the English word version of the Int value
module Main where
import Data.List (intercalate)
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 _ = "NaN"
digits :: Int -> [Int]
digits n = go n []
where
go 0 acc = acc
go x acc = go d (m:acc)
where (d, m) = divMod x 10
wordNumber :: Int -> String
wordNumber = intercalate "-" . map digitToWord . digits
main :: IO ()
main = print $ wordNumber 12324546
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment