Hi,
I am trying to solve some puzzles in F#. I am a bit confused by the way its if works. I got that if is a function with return parameter and return datatype and that it should be consistent between al cases. I am trying to translate this code to F#
int a = 0;
int b = 0;
int n = int.Parse(Console.ReadLine());
{...}
for (int i = 0; i < n; i++)
{
int c = int.Parse(inputs[i]);
if (c > a)
{
a = c;
}
else if (c < b)
{
b = c - a;
}
}
{...}
Console.Writeline(a);
After a bit of struggling I finally got a code that does compile:
let a = 0
let b = 0
let n = int(Console.In.ReadLine())
{...}
for i in 0 .. n - 1 do
let c = int(words.[i])
let temp = c - a
a = if c > a then c else a
b = if c < b then temp else b
()
{...}
printfn "%d" a
As I said, the code compiles, however printfn always return the initialisation value of a. This, despite the fact that c is bigger than a at some iterations. The debug output used at the end of the for loop shows that a is always constant. What am I doing wrong?