Reputation: 71
I want to modify an existing array with this function but for a reason I don't understand once I apply the function to the array the values do not change. What am i missing ?
Thanks in advance
let test v (a,b) =
for i=a to b do
if ((Array.get v i)>(Array.get v i+1)) then
let tmp = (Array.get v i) in
v.(i)<-(Array.get v i+1);
v.(i+1)<-tmp;
done;;
Upvotes: 0
Views: 2945
Reputation: 9030
The best practice is to put white spaces at function applications including binary operators.
Many newcomers tend to misunderstand
Array.get v i+1
as Array.get v (i+1)
but if it is written like
Array.get v i + 1
then the chance of mistake should go much lower.
Upvotes: 4
Reputation: 4425
The error is in Array.get v i+1 : this infers that v is an int array, and that you add 1 and make the condition always false. Just put i+1 in parenthesis : v will be an array of any type, and it will solve your issue.
let test v (a,b) =
for i=a to b do
if Array.get v i > Array.get v (i+1) then
let tmp = (Array.get v i) in
v.(i)<-(Array.get v (i+1));
v.(i+1)<-tmp;
done;;
You could have written using v.(i) > v.(i+1).
Upvotes: 3