[Git][monnier/typer][master] [Myers] Add nth_opt and reverse.
Simon Génier pushed to branch master at Stefan / Typer Commits: 952f92e3 by Simon Génier at 2021-03-19T09:45:17-04:00 [Myers] Add nth_opt and reverse. - - - - - 1 changed file: - src/myers.ml Changes: ===================================== src/myers.ml ===================================== @@ -75,6 +75,11 @@ let rec nthcdr n l = let nth n l = car (nthcdr n l) +let nth_opt (n : int) (l : 'a myers) : 'a option = + match nthcdr n l with + | Mnil -> None + | Mcons (x, _, _, _) -> Some x + (* While `nth` is O(log N), `set_nth` is O(N)! :-( *) let rec set_nth n v l = match (n, l) with | 0, Mcons (_, cdr, s, tail) -> Mcons (v, cdr, s, tail) @@ -136,3 +141,6 @@ let rec fold_right f l i = match l with let map f l = fold_right (fun x l' -> cons (f x) l') l nil let iteri f l = fold_left (fun i x -> f i x; i + 1) 0 l + +let reverse (l : 'a myers) : 'a myers = + fold_left (fun cdr car -> cons car cdr) nil l View it on GitLab: https://gitlab.com/monnier/typer/-/commit/952f92e33c473c43327ee180f49e046685... -- View it on GitLab: https://gitlab.com/monnier/typer/-/commit/952f92e33c473c43327ee180f49e046685... You're receiving this email because of your account on gitlab.com.
Afficher les réponses par date
participants (1)
-
Simon Génier