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:

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.


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


let main args =
    // Your stuff here


where args

will be output as string[]

. If you feel verbose, you can write this:

let main (args : string[]) =
    // Your stuff here


Everything else is fine.



All Articles