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'功能 –