如何在Scheme中计算一个数的各位数字之和?
我想计算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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
另一种方法是使用模来循环数字。我不太习惯方案语法,但是感谢 @bearzk 翻译了我的 Lisp,这里有一个适用于非负整数的函数(只需做一点工作就可以包含小数和负值):
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):
像这样的东西可以用算术方式而不是字符串方式来完成你的数字事情:
无论如何,我不知道它是否是你正在做的事情,但这个问题让我想到欧拉计划。如果是这样,您将在未来的问题中欣赏到这两个功能。
上面是最难的部分,这是其余的:
或
编辑 - 我摆脱了上面的
intLength
,但以防万一你仍然想要它。Something like this can do your digits thing arithmetically rather than string style:
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:
OR
EDIT - I got rid of
intLength
above, but in case you still want it.那些#\1、#\2 的东西就是字符。我讨厌 RTFM 你,但是这里的 Racket 文档真的很好。如果您在 DrRacket 中突出显示 string->list 并按 F1,您应该会看到一个包含大量有用信息的浏览器窗口。
以免让您蒙在鼓里;我想我可能会使用“字符串”函数作为解决方案中缺少的步骤:
...产生
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:
... produces
更好的想法是实际找到数字并对它们求和。
34%10
给出4
,3%10
给出3
。总和为3+4
。这是 F# 中的一个算法(抱歉,我不知道Scheme):
A better idea would be to actually find the digits and sum them.
34%10
gives4
and3%10
gives3
. Sum is3+4
.Here's an algorithm in F# (I'm sorry, I don't know Scheme):
这是可行的,它建立在您最初的 string->list 解决方案的基础上,只需对字符列表进行转换
即可分解转换函数以使其更加清晰:
This works, it builds on your initial string->list solution, just does a conversion on the list of characters
The conversion function could factored out to make it a little more clear:
递归过程..终止于
n < 10
其中sum-of-digits
返回输入num
本身。recursive process.. terminates at
n < 10
wheresum-of-digits
returns the inputnum
itself.