Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ccorcos
Created March 28, 2017 17:55
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 ccorcos/c3f9ac447e07ba7bf00a4d8082ae3618 to your computer and use it in GitHub Desktop.
Save ccorcos/c3f9ac447e07ba7bf00a4d8082ae3618 to your computer and use it in GitHub Desktop.
let rec nth l n =>
switch l {
| [] => None
| [h, ...t] => n === 0 ? Some h : nth t (n - 1)
};
let x = nth [0, 1, 2, 3] 999; /* option int: None */
switch x {
| Some i => print_int i
| None => print_string "not found!"
};
let y = nth [0, 1, 2, 3] 1; /* option int: Some 1 */
switch y {
| Some i => print_int i
| None => print_string "not found!"
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment