for-loop - F# for 循环错误

标签 for-loop f# type-mismatch

我在玩 F# 时发现了一个我无法理解的问题。假设我想从用户那里获取一系列整数输入并将其存储在一个数组中:

[<EntryPoint>]
let main =
    let n = Console.ReadLine() |> int
    let inputVals = Array.zeroCreate n
    for i in 0 .. n - 1 do
        inputVals.[i] <- (Console.ReadLine() |> int)
    printf "%A\n" inputVals 
    0 //compiler error here. The type does not match the expected type

但这给出了错误

This expression was expected to have type string [] -> int but here has type int

经过一番尝试后,我怀疑错误来自 for 循环。但我不明白为什么它需要一个 string[] -> int。这似乎是一个非常简单的问题,但我可以弄清楚发生了什么。

最佳答案

这里的错误与循环本身无关。您正在使用 main 作为值,但它应该是一个从字符串数组到 int函数

[<EntryPoint>]
let main args =
    // Your stuff here
    0

其中 args 将被推断为 string[]。 如果你觉得冗长,你可以拼写出来:

[<EntryPoint>]
let main (args : string[]) =
    // Your stuff here
    0

其他一切正常

关于for-loop - F# for 循环错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25914636/

相关文章:

java - 双倍数字

java - 类型不匹配 : cannot convert from element type Object to Cookie

使用 F# 进行 XML 序列化

c++ - 关闭函数时返回 vs break?

php - mySQL for 循环在 31 行更新后停止工作 [实际上与 PHP 相关]

python - 如何打印(加入)2 个循环的结果

.net - 不使用 msbuild 的 F# 编译

f# - 没有抽象成员的抽象类的对象表达式

excel - 为什么这个暗淡定义错误?

C++ : Trouble with a nested if loop not ending properly