You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Scheme中实现NumToStr过程的技术疑问:整数拆分与量级判断

Solving the NumToStr Problem in Scheme

Hey there! Let’s work through building your NumToStr procedure step by step. You’ve got two key questions—splitting an integer into its component parts and handling number magnitudes like tens, hundreds, thousands—and I’ll break both down with concrete examples and code.

1. Splitting an Integer Without Lists

You don’t need to convert the integer to a list to work with its digits or segments! Scheme’s built-in quotient and remainder functions are perfect for this. Here’s how they work:

  • (quotient n 10) gives you the "left" part of the number when divided by 10 (e.g., (quotient 82 10) returns 8)
  • (remainder n 10) gives you the rightmost digit (e.g., (remainder 82 10) returns 2)

For larger magnitudes, just adjust the divisor:

  • Use 100 to split off the hundreds place ((quotient 123 100) returns 1, (remainder 123 100) returns 23)
  • Use 1000 for thousands, and so on.

2. Handling Number Magnitudes

The trick here is to split the problem into recursive cases based on the number’s size. We’ll handle smaller, simpler ranges first, then build up to larger magnitudes:

  • 0-19: These have unique words (one, two, ..., nineteen) so we can map them directly.
  • 20-99: Combine a tens word (twenty, thirty, ..., ninety) with a units word (if the units digit isn’t zero).
  • 100-999: Handle the hundreds digit, add "HUNDRED", then recursively process the remaining two digits.
  • 1000+: Handle the thousands segment, add "THOUSAND", then recursively process the remaining three digits (repeat this pattern for millions, billions, etc.).

Full Code Example

Let’s put this all together with working Scheme code:

First, define the base word mappings:

; Words for 0-19 (we'll use 0 for edge cases like 100)
(define digit-words
  '("ZERO" "ONE" "TWO" "THREE" "FOUR" "FIVE" "SIX" "SEVEN" "EIGHT" "NINE"
    "TEN" "ELEVEN" "TWELVE" "THIRTEEN" "FOURTEEN" "FIFTEEN" "SIXTEEN"
    "SEVENTEEN" "EIGHTEEN" "NINETEEN"))

; Words for tens place (index 0 and 1 are empty since we handle 0-19 separately)
(define ten-words
  '("" "" "TWENTY" "THIRTY" "FORTY" "FIFTY" "SIXTY" "SEVENTY" "EIGHTY" "NINETY"))

Now the main NumToStr procedure with recursive logic:

(define (NumToStr n)
  (cond
    ; Case 1: 0-19 (direct word lookup)
    ((< n 20) (list (list-ref digit-words n)))
    
    ; Case 2: 20-99 (tens + optional units)
    ((< n 100)
     (let ((ten-digit (quotient n 10))
           (unit-digit (remainder n 10)))
       (if (zero? unit-digit)
           (list (list-ref ten-words ten-digit))
           (append (list (list-ref ten-words ten-digit)) (NumToStr unit-digit)))))
    
    ; Case 3: 100-999 (hundreds + "HUNDRED" + remaining digits)
    ((< n 1000)
     (let ((hundred-digit (quotient n 100))
           (rest (remainder n 100)))
       (if (zero? rest)
           (append (NumToStr hundred-digit) (list "HUNDRED"))
           (append (NumToStr hundred-digit) (list "HUNDRED") (NumToStr rest)))))
    
    ; Case 4: 1000-999,999 (thousands segment + "THOUSAND" + remaining digits)
    ((< n 1000000)
     (let ((thousand-segment (quotient n 1000))
           (rest (remainder n 1000)))
       (if (zero? rest)
           (append (NumToStr thousand-segment) (list "THOUSAND"))
           (append (NumToStr thousand-segment) (list "THOUSAND") (NumToStr rest)))))
    
    ; Add more cases for millions, billions, etc., as needed
    (else (error "Number too large for NumToStr"))))

Testing It Out

  • (NumToStr 82) returns ("EIGHTY" "TWO") (matches your example!)
  • (NumToStr 123) returns ("ONE" "HUNDRED" "TWENTY" "THREE")
  • (NumToStr 4500) returns ("FOUR" "THOUSAND" "FIVE" "HUNDRED")

This approach keeps the code clean by leaning into recursion—each case handles a small chunk of the problem and passes the rest off to the same procedure.

内容的提问来源于stack exchange,提问作者zlavy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:06:07