Last active
December 14, 2015 06:09
-
-
Save notpushkin/5040725 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* 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);; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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