F # for loop error

I was playing around with F # and noticed a problem that I can't figure out. Let's say I want to receive a series of integer inputs from the user and store them in an array:

[<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

      

But this gives an error

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

      

After playing around for a while, I suspect the error is coming from the for loop. But I can't figure out why it is expecting a string [] -> int. This seems like a very simple problem, but I just can figure out what's going on.

+3


source to share


1 answer


The error here is not related to the loop itself. You are using main

as a value, but it must be a function from an array of strings in int

.

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

      

where args

will be output as string[]

. If you feel verbose, you can write this:



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

      

Everything else is fine.

+5


source







All Articles