Files
rose-ash/lib/ocaml/baseline
giles a91ff62730
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 28s
ocaml: phase 5.1 bsearch.ml baseline (binary search, position sum = 7)
Iterative binary search on a sorted int array:

  let bsearch arr target =
    let n = Array.length arr in
    let lo = ref 0 and hi = ref (n - 1) in
    let found = ref (-1) in
    while !lo <= !hi && !found = -1 do
      let mid = (!lo + !hi) / 2 in
      if arr.(mid) = target then found := mid
      else if arr.(mid) < target then lo := mid + 1
      else hi := mid - 1
    done;
    !found

For [1;3;5;7;9;11;13;15;17;19;21]:
  bsearch a 13   = 6
  bsearch a 5    = 2
  bsearch a 100  = -1
  sum            = 7

Exercises Array.of_list + arr.(i) + multi-let 'let lo = ... and
hi = ...' + while + multi-arm if/else if/else.

49 baseline programs total.
2026-05-09 10:40:49 +00:00
..