Last active
October 2, 2017 04:54
-
-
Save kunigami/7df1cc60f9d94ccfc8d7452dd726afb5 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
let rec lookup: 'a. int -> 'a seq -> 'a = | |
fun index digits -> match (index, digits) with | |
| (index, Nil) -> raise IndexOutOfBoundsException | |
(* Case 1 *) | |
| (0, One (elem, restDigits)) -> elem | |
(* Case 2 *) | |
| (index, One (_, restDigits)) -> lookup (index - 1) (Zero restDigits) | |
(* Case 3 *) | |
| (index, Zero restDigits) -> | |
let (left, right) = lookup (index / 2) restDigits | |
in if index mod 2 == 0 then left else right |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment