计数球拍列表中的位数 [英] Count digits in list Racket
本文介绍了计数球拍列表中的位数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个疑问,我正在使用球拍,我想计算列表中的数字,但我不能.我尝试使用长度,但由于我想要的而无法正常工作,因为
I have a doubt, I'm using Racket, and I wanna count the digits of a list, but I can't. I try with length but it doesn't work as I want because
(countDigits'(4 5 6 78))> 5
(countDigits '(4 5 6 78)) > 5
答案必须是5,但是,我不知道该怎么做,我有一个数字来计数数字,但是我不知道如何在列表中进行编码.¿我该怎么办?
the answer has to be 5 but, i don't know how to, i have a code for count digits in a number but I don't knowhow to do it in a list. ¿How could I do it?
推荐答案
以下是可能的解决方案:
Here's a possible solution:
(define (countDigits lst)
(apply +
(map (compose string-length number->string)
lst)))
说明:
- 对于列表中的每个数字,我们将其转换为字符串
- 然后,我们获得每个字符串的长度-这将告诉我们数字位数
- 最后,我们将所有长度加在一起
例如:
(countDigits '(4 5 6 78))
=> 5
这篇关于计数球拍列表中的位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文