對不起愚蠢的問題,但我有點困惑。
如何重複TryParse直到F#成功?
在C#中,我可以慣用做到以下幾點:
int result = 0;
while (!Int32.TryParse(someString, out result))
{
...
}
在F#我有嘗試DoSomething的模式兩種選擇。
它要麼
let (isSuccess, result) = Int32.TryParse someString
或
let result = ref 0
let isSuccess = Int32.TryParse("23", result)
我可以做while not Int32.TryParse("23", result) do ...
,但不知道是否相同與第一方案可以實現的。
P.S.當然,尾遞歸也是可行這裏,但我感興趣的是使用while
結構。
我想你已經回答了你的問題:)你可以使用第二個變體與'而'或使用遞歸的第一個變種... –