Search⌘ K

Solution Review: Find the Minimum in an Array

Explore how to use for loops in ReasonML to find the minimum number in an array. This lesson reviews a solution approach that initializes a mutable minimum and updates it while iterating through the array. You will gain understanding of loops and mutable variables in ReasonML through practical example.

We'll cover the following...

Solution

Reason
let arr = [| 23, 45, 17, 10, 60, 54 |];
let findMin = (arr: array(int)) => {
let min = ref(-1);
min := arr[0]; /* Start with the first value of the array */
for (i in 1 to Array.length(arr)-1) { /* Travrse the array */
if (min^ > arr[i]) { /* Whenever a value smaller than min is found */
min := arr[i]; /* Make it the new min */
};
};
min^;
};
Js.log(findMin(arr));

Explanation

The for ...