Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# for loop error

I was playing around F# and noticed a problem I cannot understand. Say I want get a series of integer inputs from the user and store it 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 the error

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

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

like image 506
mashrur Avatar asked Nov 25 '25 03:11

mashrur


1 Answers

The error here is unrelated to the loop itself. You are using main as a value, but it should be a function from array of strings into int.

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

where args will be inferred as string[]. If you are feeling verbose, you can spell it out:

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

Everything else is fine.

like image 95
Patryk Ćwiek Avatar answered Nov 27 '25 20:11

Patryk Ćwiek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!