Skip to content

Instantly share code, notes, and snippets.

@qexat
Last active November 14, 2025 21:21
Show Gist options
  • Select an option

  • Save qexat/8d21b9824d643936b50c13fbe02df8c4 to your computer and use it in GitHub Desktop.

Select an option

Save qexat/8d21b9824d643936b50c13fbe02df8c4 to your computer and use it in GitHub Desktop.
this is the future jane street wants. #NoToNil #protest #Option4Ever #foryou #foryoupage
type nil = Nil
(* This version is unsafe because it makes the assumption that index >= 0. *)
let rec get_unsafe : 'a. at:int -> from:'a list -> 'a | nil =
fun ~at:index ~from:list ->
match list with
| [] -> Nil
| first :: _ when index = 0 -> first
| _ :: rest -> get_unsafe ~at:(index - 1) ~from:rest
let rec get : 'a. at:int -> from:'a list -> 'a | nil =
fun ~at:index ~from:list ->
if index >= 0
then get_unsafe ~at:index ~from:list
else get_unsafe ~at:(List.length list + index) ~from:list
let get_exn : 'a. at:int -> from:'a list -> 'a =
fun ~at ~from ->
let value = get ~at ~from in
match type of value with
| nil -> failwith "get_exn: index out of bounds"
| _ -> value
@qexat
Copy link
Author

qexat commented Nov 14, 2025

To be clear, this is not valid OCaml code, it's just a (bad) proof of concept

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment