I've faced confusing for me difference between output in F# interactive and output on Console.
Overview of the problem:
Given an array A of length n Sort the indexes in the following order:
index[i] < index[j] if (A[i] < A[j]) or (A[i] = A[j] and i < j)
For example
[| 4; 2; 4; 2; 1; 1 |]
the indexes here would be sorted like this
[| 4; 5; 1; 3; 0; 2 |]
let GetSorted (A:int[]) =
let Comparer i j =
match (A.[i], A.[j]) with
| a1, a2 when a1 > a2 -> +1
| a1, a2 when a1 < a2 -> -1
| _, _ when i > j -> +1
| _, _ -> -1
let lenA = A |> Array.length
[| 0 .. lenA - 1 |] |> Array.sortWith Comparer
let A = [| 0; 0; 0; 0; 0; 0; 0; 0; 42; 0; 0; 0; 0; 0; 0; 0; 41 |]
let sortedPositions = GetSorted A
When i run above script in F# interactive, i got the following:
val sortedPositions : int [] =
[|0; 1; 2; 3; 4; 5; 6; 7; 9; 10; 11; 12; 13; 14; 15; 16; 8|]
Notice, the last two indexes are 16 and 8. Which is correct, because 41 < 42
when i tried to do it thru console run:
sortedPositions |> Array.iter(Console.WriteLine)
[|0; 1; 2; 3; 4; 5; 6; 7; 9; 10; 11; 12; 13; 14; 15; 8; 16|]
I didnt find another length of an array, so the two runs would give different results, so far only 17 was spotted.
Why is this happening? I have a suspicion that sorting works differently in F# Interactive, because when you look closer to my Comparer, it doesnt handle properly situation with the same indexes (although i don't know why sorting algorithm requires checks for same indexes). So, when i add another match for
| _, _ when i = j -> 0
It begins to work. But still, confused that without it i'm getting different results.