Skip to content

Instantly share code, notes, and snippets.

@notpushkin
Last active December 14, 2015 06:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save notpushkin/5040725 to your computer and use it in GitHub Desktop.
Save notpushkin/5040725 to your computer and use it in GitHub Desktop.
(* bin_find.ml v2 * Ale110 * WTFPL2 *)
let bin_search a x =
let rec search_between m n =
if m >= n then
None
else
let mn = (m+n)/2 in
let comp = compare x a.[mn] in
if comp = 0 then
Some mn
else if comp > 0 then
search_between (mn+1) n
else
search_between m mn
in
search_between 0 (Array.length a);;
let bin_find = bin_search;;
(* DEBUG *)
let print_option f o =
match o with
| None -> print_string "None"
| Some x -> print_string "Some "; f x;;
print_option
(print_int)
(bin_search [|1; 4; 88; 228; 1488; 265265|] 88);;
$ ocaml ./bin_find.ml
File "./bin_find.ml", line 17, characters 33-34:
Error: This expression has type string but an expression was expected of type
'a array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment