Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Scheme function int-to-words that takes a nonnegative integer between 0

ID: 3863097 • Letter: W

Question

Write a Scheme function int-to-words that takes a nonnegative integer between 0 and 99, inclusive, as a parameter and returns a list of words corresponding to the integers reading in English. Make your function as short as possible; i.e., don’t write a COND statement with 100 tests. Example calls to your function are given below:

(int-to-words 13) ; should return (thirteen)

(int-to-words 42) ; should return (forty two)

Note, in order to simplify things, we are not expecting dashes in the output (i.e., 42 is output as forty two not forty-two). Hint: You may want to use the built-in integer division functions quotient and remainder.

Explanation / Answer

(define (int-to-words n)
(define 1to19 '(one two three four five six seven eight nine ten eleven twelve
thirteen fourteen fifteen sixteen seventeen eighteen nineteen))
(define 10_multipls '(twenty thirty forty fifty sixty seventy eighty ninety))
(define hundreds '(hundred ))
(cond
((= n 0) '(zero)) ; zero is a special case since from now on all 0 will be suppressed
((< n 0) (cons 'minus (int-to-words (- n))))
(else
(let loop ((n n) (units hundreds) (res '()))
(cond
; --- below 1000
((= n 0) res)
((< 0 n 20) (cons (list-ref 1to19 (- n 1)) res))
((< n 100) (cons (list-ref 10_multipls (- (quotient n 10) 2))
(loop (remainder n 10) '() res)))
(else
; --- 100 and above
(let ((q (quotient n 100))
(res (loop (remainder n 100) hundreds res)))
(if (zero? q)
res
(loop q (cdr units) (cons (car units) res))))))))))

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote