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