如何在Scheme中计算一个数的各位数字之和?

发布于 2024-11-15 20:23:13 字数 538 浏览 1 评论 0原文

我想计算Scheme中一个数字的数字之和。它应该像这样工作:

>(sum-of-digits 123)
 6

我的想法是将数字 123 转换为字符串 "123",然后将其转换为列表 '(1 2 3)< /code> 然后使用 (apply + '(1 2 3)) 得到 6

但不幸的是它并没有像我想象的那样工作。

>(string->list(number->string 123))
'(#\1 #\2 #\3)

显然 '(#\1 #\2 #\3)'(1 2 3) 不同...因为我使用的是语言 racket在DrRacket下,所以我不能使用像char->digit这样的功能。

谁能帮我解决这个问题吗?

I want to calculate the sum of digits of a number in Scheme. It should work like this:

>(sum-of-digits 123)
 6

My idea is to transform the number 123 to string "123" and then transform it to a list '(1 2 3) and then use (apply + '(1 2 3)) to get 6.

but it's unfortunately not working like I imagined.

>(string->list(number->string 123))
'(#\1 #\2 #\3)

Apparently '(#\1 #\2 #\3) is not same as '(1 2 3)... because I'm using language racket under DrRacket, so I can not use the function like char->digit.

Can anyone help me fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

樱桃奶球 2024-11-22 20:23:13

另一种方法是使用模来循环数字。我不太习惯方案语法,但是感谢 @bearzk 翻译了我的 Lisp,这里有一个适用于非负整数的函数(只需做一点工作就可以包含小数和负值):

(define (sum-of-digits x) 
  (if (= x 0) 0 
      (+ (modulo x 10) 
         (sum-of-digits (/ (- x (modulo x 10)) 10)))))

An alternative method would be to loop over the digits by using modulo. I'm not as used to scheme syntax, but thanks to @bearzk translating my Lisp here's a function that works for non-negative integers (and with a little work could encompass decimals and negative values):

(define (sum-of-digits x) 
  (if (= x 0) 0 
      (+ (modulo x 10) 
         (sum-of-digits (/ (- x (modulo x 10)) 10)))))
此岸叶落 2024-11-22 20:23:13

像这样的东西可以用算术方式而不是字符串方式来完成你的数字事情:

(define (digits n)
    (if (zero? n)
        '()
        (cons (remainder n 10) (digits2 (quotient n 10))))

无论如何,我不知道它是否是你正在做的事情,但这个问题让我想到欧拉计划。如果是这样,您将在未来的问题中欣赏到这两个功能。

上面是最难的部分,这是其余的:

(foldr + (digits 12345) 0)

(apply + (digits 1234))

编辑 - 我摆脱了上面的 intLength ,但以防万一你仍然想要它。

(define (intLength x)
   (define (intLengthP x c)
      (if (zero? x)
          c
          (intLengthP (quotient x 10) (+ c 1))
      )
   )
   (intLengthP x 0))

Something like this can do your digits thing arithmetically rather than string style:

(define (digits n)
    (if (zero? n)
        '()
        (cons (remainder n 10) (digits2 (quotient n 10))))

Anyway, idk if its what you're doing but this question makes me think Project Euler. And if so, you're going to appreciate both of these functions in future problems.

Above is the hard part, this is the rest:

(foldr + (digits 12345) 0)

OR

(apply + (digits 1234))

EDIT - I got rid of intLength above, but in case you still want it.

(define (intLength x)
   (define (intLengthP x c)
      (if (zero? x)
          c
          (intLengthP (quotient x 10) (+ c 1))
      )
   )
   (intLengthP x 0))
全部不再 2024-11-22 20:23:13

那些#\1、#\2 的东西就是字符。我讨厌 RTFM 你,但是这里的 Racket 文档真的很好。如果您在 DrRacket 中突出显示 string->list 并按 F1,您应该会看到一个包含大量有用信息的浏览器窗口。

以免让您蒙在鼓里;我想我可能会使用“字符串”函数作为解决方案中缺少的步骤:

(map string (list #\a #\b))

...产生

(list "a" "b")

Those #\1, #\2 things are characters. I hate to RTFM you, but the Racket docs are really good here. If you highlight string->list in DrRacket and hit F1, you should get a browser window with a bunch of useful information.

So as not to keep you in the dark; I think I'd probably use the "string" function as the missing step in your solution:

(map string (list #\a #\b))

... produces

(list "a" "b")
写下不归期 2024-11-22 20:23:13

更好的想法是实际找到数字并对它们求和。 34%10 给出 43%10 给出 3。总和为3+4

这是 F# 中的一个算法(抱歉,我不知道Scheme):

let rec sumOfDigits n =
    if n<10 then n
    else (n%10) + sumOfDigits (n/10)

A better idea would be to actually find the digits and sum them. 34%10 gives 4 and 3%10 gives 3. Sum is 3+4.

Here's an algorithm in F# (I'm sorry, I don't know Scheme):

let rec sumOfDigits n =
    if n<10 then n
    else (n%10) + sumOfDigits (n/10)
守望孤独 2024-11-22 20:23:13

这是可行的,它建立在您最初的 string->list 解决方案的基础上,只需对字符列表进行转换

(apply + (map (lambda (d) (- (char->integer d) (char->integer #\0)))
       (string->list (number->string 123))))

即可分解转换函数以使其更加清晰:

(define (digit->integer d)
  (- (char->integer d) (char->integer #\0)))

(apply + (map digit->integer (string->list (number->string 123))))

This works, it builds on your initial string->list solution, just does a conversion on the list of characters

(apply + (map (lambda (d) (- (char->integer d) (char->integer #\0)))
       (string->list (number->string 123))))

The conversion function could factored out to make it a little more clear:

(define (digit->integer d)
  (- (char->integer d) (char->integer #\0)))

(apply + (map digit->integer (string->list (number->string 123))))
纵性 2024-11-22 20:23:13
(define (sum-of-digits num)
    (if (< num 10)
        num
        (+ (remainder num 10) (sum-of-digits (/ (- num (remainder num 10)) 10)))))

递归过程..终止于n < 10 其中 sum-of-digits 返回输入 num 本身。

(define (sum-of-digits num)
    (if (< num 10)
        num
        (+ (remainder num 10) (sum-of-digits (/ (- num (remainder num 10)) 10)))))

recursive process.. terminates at n < 10 where sum-of-digits returns the input num itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文