Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Turns an integer into an array of strings that correspond to the correct wav file names to read out.
let emptyIfZero f x =
match x with
| 0 -> []
| _ -> f x
let elementsBetween0And20 = emptyIfZero (fun x -> [ string x ])
let elementsBetween21And99 x = string (x - (x % 10)) :: elementsBetween0And20 (x % 10)
let ifGreaterThan y f g x = if x > y then f x else g x
let elementsFor2DigitNumber = ifGreaterThan 20 elementsBetween21And99 elementsBetween0And20
let elementsBetween100And999 x = string (x / 100) :: "hundred" :: emptyIfZero (fun x -> "and" :: elementsFor2DigitNumber x) (x % 100)
let elementsFor3DigitNumber = ifGreaterThan 99 elementsBetween100And999 elementsFor2DigitNumber
let generateSeparatorText n x y =
match n with
| "thousand" when y > 0 && y < 100 && x % 1000 > 0 -> [ n; "and" ]
| "thousand" when y > 0 && y < 100 -> [ "and" ]
| _ when x % 1000 = 0 -> []
| _ -> [ n ]
let rec generateElementsRec l x =
match l with
| n::ns when x > 999 -> elementsFor3DigitNumber (x % 1000) :: generateSeparatorText n (x / 1000) (x % 1000) :: generateElementsRec ns (x / 1000)
| _ -> [ elementsFor3DigitNumber x ]
let generateElements x =
match x with
| 0 -> [ "0" ]
| _ when x < 0 -> raise <| System.ArgumentOutOfRangeException()
| _ ->
generateElementsRec [ "thousand"; "million"; "billion" ] x
|> List.rev
|> List.collect id
@Kntajus

This comment has been minimized.

Copy link
Owner Author

Kntajus commented Feb 28, 2014

First 3 revisions of this were buggy! (Try 5000076 as input)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.