4
我想弄清楚一个很好的方法来检查一个字符串是否只包含数字。这是我努力的结果,但它似乎非常啰嗦:F#检查一个字符串是否只包含数字
let isDigit c = Char.IsDigit c
let rec strContainsOnlyNumber (s:string)=
let charList = List.ofSeq s
match charList with
| x :: xs ->
if isDigit x then
strContainsOnlyNumber (String.Concat (Array.ofList xs))
else
false
| [] -> true
例如,它似乎真的很丑,我有一个字符串为char列表,然后返回一个字符串转换。 你能想出更好的解决方案吗?
这是功课吗?无论如何,我会使用'Seq.forall'。 – kvb
哇,好吧,让我们说这是作业,但实际上我只是从C#切换。 Seq.forall(fun elem - > isDigit elem)“12345”这个工作也好,它更好 – Zeta
你可以缩短到'Seq.forall Char.IsDigit“12345”'而不需要中间的'isDigit'功能 –