Skip to content

Instantly share code, notes, and snippets.

@ccorcos

ccorcos/nth2.re

Created Mar 28, 2017
Embed
What would you like to do?
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
You can’t perform that action at this time.