问题
I have the function below which converts an input of numbers into the partially translated word output of those numbers.
Using product and quotient, it adds the word representation of numbers while splitting the number into groups.
For example:
(number-name 87969087) -> '(87 million 969 thousand 87)
(number-name 1000000) -> '(1 million)
Im trying to complete my problem by fully translating those numbers which are less than 1000 as well. Im trying to implement a function less-than-1000 which will display those smaller numbers as the list is being constructed as well. Alongside:
;; for less than 1000
; state words for 1-19
(define baseNumbers '(one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen))
and
; state words for multiples of ten
(define multiples '(twenty thirty forty fifty sixty seventy eighty ninety))
so
(number-name 1110) -> '(one thousand one hundred ten)
Its also been difficult coming up with a way to display an input of 0 to display as zero by do so in a way in which zero does not show up if the input is anything other than 0.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
(define (number n)
(define units '(thousand million billion trillion quadrillion))
(define (nsplit n units acc lst)
(define q (quotient n 1000))
(define r (remainder n 1000))
(if (zero? n) lst
(cond [(zero? acc)
(if (zero? r)
(nsplit q units (add1 acc) lst)
(nsplit q units (add1 acc) (cons r lst)))]
[(zero? r)
(nsplit q (cdr units) acc lst)]
[else
(nsplit q (cdr units) acc (cons r (cons (car units) lst)))])))
(nsplit n units 0 empty))
回答1:
You can translate an integer into a list of symbols by breaking apart the number into 3-digit groups, attaching units to each group, and then further translating the 3-digit groups to list of symbols. Here is a sample implementation:
(define (num->lst num)
(define bases '(one two three four five six seven eight nine ten eleven twelve
thirteen fourteen fifteen sixteen seventeen eighteen nineteen))
(define multiples '(twenty thirty forty fifty sixty seventy eighty ninety))
(define units '(empty thousand million billion trillion quadrillion quintillion
sextillion septillion octillion nonillion decillion))
(define (below-1000 num bases mults)
(cond [(zero? num) empty]
[(< num 20) (list (list-ref bases (sub1 num)))]
[(< num 100) (list* (list-ref mults (- (quotient num 10) 2))
(below-1000 (remainder num 10) bases mults))]
[else (list* (list-ref bases (sub1 (quotient num 100))) 'hundred
(below-1000 (remainder num 100) bases mults))]))
(define (nsplit num lst units)
(define q (quotient num 1000))
(define r (remainder num 1000))
(if (zero? num) lst
(cond [(zero? r) (nsplit q lst (cdr units))]
[else (nsplit q (append (below-1000 r bases multiples)
(cons (car units) lst)) (cdr units))])))
(if (zero? num) '(zero)
(remove 'empty (nsplit num empty units))))
below-1000
converts numbers below 1000 into list of symbols.
nsplit
breaks the number into 3-digit groups and attaches units to each group, while converting the 3-digits using below-1000
.
For example,
> (num->lst 0)
'(zero)
> (num->lst 1000000001)
'(one billion one)
> (num->lst 35579005)
'(thirty five million five hundred seventy nine thousand five)
来源:https://stackoverflow.com/questions/42660997/converting-numbers-to-english-letter-list