抱歉,如果我的标题描述性不够,我不知道如何表达这个问题……我习惯用 C# 编程,并且一直在涉足 F#。我正在尝试编写一个在某些 C# 脚本中使用的函数,用于检查字符串是否为数字。我有一个像这样写的 F# 函数,尽管根据 VS 它不正确,因为它期待一个 else:
let IsNumeric (entry : string) : bool = // Function to make sure the data entered is numeric
for c : char in entry do
if not ((c >= '0') && (c <= '9')) then
false
true
如果我放入 else 语句并删除底部的 true :
let IsNumeric (entry : string) : bool = // Function to make sure the data entered is numeric
for c : char in entry do
if not ((c >= '0') && (c <= '9')) then
false
else true
我收到这个错误
FS0001 此表达式的类型应为“bool”,但此处的类型为“unit”
...如果我像在第一个代码块中一样在底部保持 true ,我会收到关于它返回 bool 的警告,但应该忽略我不太明白的。
FS0020 此表达式的结果类型为“bool”并被隐式忽略。考虑使用'ignore' 显式丢弃该值,例如'expr |> ignore',或'let' 将结果绑定到一个名称,例如'let result = expr'。
这是我一直在尝试适应的 C# 方法:
public static bool IsNumeric(string s) //this just makes sure the string is numeric.
{
foreach (char c in s)
{
if (!(c >= '0' && c <= '9') && c != '.' && c != '-')
{
return false;
}
}
return true;
}
我应该如何处理这个?