-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheque.hs
429 lines (399 loc) · 11.9 KB
/
Cheque.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-
Write a function (dollars) that accepts a `String` and returns a `String`.
It will accept a numeric value as input, representing an amount of money, and convert to its transcribed English.
For example, the input "1.11" will result in a return value of "one dollar and eleven cents"
Invalid characters should be ignored, meaning that every input string has an output string.
The empty string produces "zero dollars and zero cents"
There is a `test` function below that lists more examples of input and output. There are also functions and
data structures that may assist you in deriving the result. It is not compulsory that they are used.
-}
module Cheque where
import qualified Applicative as A
import Core
import List (Chars, List (..))
import qualified List as L
import qualified Monad as M
-- $setup
-- >>> :set -XOverloadedStrings
-- The representation of the grouping of each exponent of one thousand. ["thousand", "million", ...]
illion ::
List Chars
illion =
let preillion ::
List (Chars -> Chars)
preillion =
L.listh
[ const "",
const "un",
const "do",
const "tre",
const "quattuor",
const "quin",
const "sex",
const "septen",
const "octo",
\q -> if "n" `L.isPrefixOf` q then "novem" else "noven"
]
postillion ::
List Chars
postillion =
L.listh
[ "vigintillion",
"trigintillion",
"quadragintillion",
"quinquagintillion",
"sexagintillion",
"septuagintillion",
"octogintillion",
"nonagintillion",
"centillion",
"decicentillion",
"viginticentillion",
"trigintacentillion",
"quadragintacentillion",
"quinquagintacentillion",
"sexagintacentillion",
"septuagintacentillion",
"octogintacentillion",
"nonagintacentillion",
"ducentillion",
"deciducentillion",
"vigintiducentillion",
"trigintaducentillion",
"quadragintaducentillion",
"quinquagintaducentillion",
"sexagintaducentillion",
"septuagintaducentillion",
"octogintaducentillion",
"nonagintaducentillion",
"trecentillion",
"decitrecentillion",
"vigintitrecentillion",
"trigintatrecentillion",
"quadragintatrecentillion",
"quinquagintatrecentillion",
"sexagintatrecentillion",
"septuagintatrecentillion",
"octogintatrecentillion",
"nonagintatrecentillion",
"quadringentillion",
"deciquadringentillion",
"vigintiquadringentillion",
"trigintaquadringentillion",
"quadragintaquadringentillion",
"quinquagintaquadringentillion",
"sexagintaquadringentillion",
"septuagintaquadringentillion",
"octogintaquadringentillion",
"nonagintaquadringentillion",
"quingentillion",
"deciquingentillion",
"vigintiquingentillion",
"trigintaquingentillion",
"quadragintaquingentillion",
"quinquagintaquingentillion",
"sexagintaquingentillion",
"septuagintaquingentillion",
"octogintaquingentillion",
"nonagintaquingentillion",
"sescentillion",
"decisescentillion",
"vigintisescentillion",
"trigintasescentillion",
"quadragintasescentillion",
"quinquagintasescentillion",
"sexagintasescentillion",
"septuagintasescentillion",
"octogintasescentillion",
"nonagintasescentillion",
"septingentillion",
"deciseptingentillion",
"vigintiseptingentillion",
"trigintaseptingentillion",
"quadragintaseptingentillion",
"quinquagintaseptingentillion",
"sexagintaseptingentillion",
"septuagintaseptingentillion",
"octogintaseptingentillion",
"nonagintaseptingentillion",
"octingentillion",
"decioctingentillion",
"vigintioctingentillion",
"trigintaoctingentillion",
"quadragintaoctingentillion",
"quinquagintaoctingentillion",
"sexagintaoctingentillion",
"septuagintaoctingentillion",
"octogintaoctingentillion",
"nonagintaoctingentillion",
"nongentillion",
"decinongentillion",
"vigintinongentillion",
"trigintanongentillion",
"quadragintanongentillion",
"quinquagintanongentillion",
"sexagintanongentillion",
"septuagintanongentillion",
"octogintanongentillion",
"nonagintanongentillion"
]
in L.listh
[ "",
"thousand",
"million",
"billion",
"trillion",
"quadrillion",
"quintillion",
"sextillion",
"septillion",
"octillion",
"nonillion",
"decillion",
"undecillion",
"duodecillion",
"tredecillion",
"quattuordecillion",
"quindecillion",
"sexdecillion",
"septendecillion",
"octodecillion",
"novemdecillion"
]
L.++ A.lift2 ((L.++) M.=<<) preillion postillion
-- A data type representing the digits zero to nine.
-- data Digit
-- = Zero
-- | One
-- | Two
-- | Three
-- | Four
-- | Five
-- | Six
-- | Seven
-- | Eight
-- | Nine
-- deriving stock (Eq, Ord, Show)
-- showDigit :: Digit -> Chars
-- showDigit = \case
-- Zero -> "zero"
-- One -> "one"
-- Two -> "two"
-- Three -> "three"
-- Four -> "four"
-- Five -> "five"
-- Six -> "six"
-- Seven -> "seven"
-- Eight -> "eight"
-- Nine -> "nine"
-- -- A data type representing one, two or three digits, which may be useful for grouping.
-- data Digit3
-- = D1 Digit
-- | D2 Digit Digit
-- | D3 Digit Digit Digit
-- deriving stock (Eq)
-- -- Possibly convert a character to a digit.
-- fromChar :: Char -> Optional Digit
-- fromChar = \case
-- '0' -> Full Zero
-- '1' -> Full One
-- '2' -> Full Two
-- '3' -> Full Three
-- '4' -> Full Four
-- '5' -> Full Five
-- '6' -> Full Six
-- '7' -> Full Seven
-- '8' -> Full Eight
-- '9' -> Full Nine
-- _ -> Empty
units :: List Chars
units =
L.listh
[ "one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine"
]
specials :: List Chars
specials =
L.listh
[ "eleven",
"twelve",
"thirteen",
"fourteen",
"fifteen",
"sixteen",
"seventeen",
"eighteen",
"nineteen"
]
tens :: List Chars
tens =
L.listh
[ "ten",
"twenty",
"thirty",
"forty",
"fifty",
"sixty",
"seventy",
"eighty",
"ninety"
]
-- | Take a numeric value and produce its English output.
--
-- >>> dollars "0"
-- "zero dollars and zero cents"
--
-- >>> dollars "1"
-- "one dollar and zero cents"
--
-- >>> dollars "0.1"
-- "zero dollars and ten cents"
--
-- >>> dollars "1."
-- "one dollar and zero cents"
--
-- >>> dollars "0."
-- "zero dollars and zero cents"
--
-- >>> dollars "0.0"
-- "zero dollars and zero cents"
--
-- >>> dollars ".34"
-- "zero dollars and thirty-four cents"
--
-- >>> dollars "0.3456789"
-- "zero dollars and thirty-four cents"
--
-- >>> dollars "1.0"
-- "one dollar and zero cents"
--
-- >>> dollars "1.01"
-- "one dollar and one cent"
--
-- >>> dollars "a1a"
-- "one dollar and zero cents"
--
-- >>> dollars "a1a.a0.7b"
-- "one dollar and seven cents"
--
-- >>> dollars "100"
-- "one hundred dollars and zero cents"
--
-- >>> dollars "100.0"
-- "one hundred dollars and zero cents"
--
-- >>> dollars "100.00"
-- "one hundred dollars and zero cents"
--
-- >>> dollars "100.00000"
-- "one hundred dollars and zero cents"
--
-- >>> dollars "1000456.13"
-- "one million four hundred and fifty-six dollars and thirteen cents"
--
-- >>> dollars "1001456.13"
-- "one million one thousand four hundred and fifty-six dollars and thirteen cents"
--
-- >>> dollars "16000000456.13"
-- "sixteen billion four hundred and fifty-six dollars and thirteen cents"
--
-- >>> dollars "100.45"
-- "one hundred dollars and forty-five cents"
--
-- >>> dollars "100.07"
-- "one hundred dollars and seven cents"
--
-- >>> dollars "9abc9def9ghi.jkl9mno"
-- "nine hundred and ninety-nine dollars and ninety cents"
--
-- >>> dollars "12345.67"
-- "twelve thousand three hundred and forty-five dollars and sixty-seven cents"
--
-- >>> dollars "456789123456789012345678901234567890123456789012345678901234567890.12"
-- "four hundred and fifty-six vigintillion seven hundred and eighty-nine novemdecillion one hundred and twenty-three octodecillion four hundred and fifty-six septendecillion seven hundred and eighty-nine sexdecillion twelve quindecillion three hundred and forty-five quattuordecillion six hundred and seventy-eight tredecillion nine hundred and one duodecillion two hundred and thirty-four undecillion five hundred and sixty-seven decillion eight hundred and ninety nonillion one hundred and twenty-three octillion four hundred and fifty-six septillion seven hundred and eighty-nine sextillion twelve quintillion three hundred and forty-five quadrillion six hundred and seventy-eight trillion nine hundred and one billion two hundred and thirty-four million five hundred and sixty-seven thousand eight hundred and ninety dollars and twelve cents"
dollars :: Chars -> Chars
dollars xs = fmt d "dollar" L.++ " and " L.++ fmt c "cent"
where
fmt amt curr
| amt == "" = "zero " L.++ curr L.++ "s"
| amt == "one" = amt L.++ " " L.++ curr
| otherwise = amt L.++ " " L.++ curr L.++ "s"
(whole, decimal) = parseDecimal xs
d = words whole
c = words decimal
words :: Chars -> Chars
words xs
| (n + k) <= 3 = lessThanThou ys
| otherwise = L.unwords (wl :. w :. (if L.isEmpty wr then Nil else wr :. Nil))
where
ys = lstrip xs
((n, l), (k, r)) = split ys
w = elemAt (k `div` 3) illion
wl = words l
wr = words r
lessThanThou :: Chars -> Chars
lessThanThou xs =
case ys of
Nil -> Nil
_ :. Nil -> elemAt (i - 1) units
_ :. "0" -> elemAt (i - 1) tens
'1' :. _ :. Nil -> elemAt (j - 1) specials
_ :. _ :. Nil -> elemAt (i - 1) tens L.++ "-" L.++ elemAt (j - 1) units
_ :. "00" -> elemAt (i - 1) units L.++ " hundred"
_ -> L.unwords (elemAt (i - 1) units :. "hundred" :. "and" :. lessThanThou r :. Nil)
where
ys = lstrip xs
(l, r) = uncons ys
i = Core.digitToInt l
j = Core.digitToInt (head r)
parseDecimal :: Chars -> (Chars, Chars)
parseDecimal xs = (whole, decimal)
where
(ys, zs) = L.span (/= '.') xs
digits = L.filter isDigit
whole = digits ys
decimal = L.take 2 (digits zs L.++ "00")
type Group a = (Int, List a)
{-
Split into (prefix, suffix) such that the length
of the suffix is the longest multiple of three.
Examples:
1001 --> (1, 001)
999999 --> (999, 999)
1000001 --> (1, 000001)
-}
split :: List a -> (Group a, Group a)
split = L.foldRight f ((0, Nil), (0, Nil))
where
f x ((k, xs), acc@(n, ys)) =
if k == 3
then ((1, x :. Nil), (n + 3, xs L.++ ys))
else ((k + 1, x :. xs), acc)
-- List helper functions.
lstrip :: Chars -> Chars
lstrip = L.dropWhile (== '0')
{-
The following are partial functions
that throw an error if the list doesn't
contain the required number of elements.
-}
elemAt :: Int -> List a -> a
elemAt n xs =
case L.drop n xs of
x :. _ -> x
_ -> error "out of range"
head :: List a -> a
head = elemAt 0
uncons :: List a -> (a, List a)
uncons Nil = error "empty list"
uncons (x :. xs) = (x, xs)