Skip to content

Instantly share code, notes, and snippets.

@kunigami
Last active October 2, 2017 04:54
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 kunigami/7df1cc60f9d94ccfc8d7452dd726afb5 to your computer and use it in GitHub Desktop.
Save kunigami/7df1cc60f9d94ccfc8d7452dd726afb5 to your computer and use it in GitHub Desktop.
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