diff --git a/Algo/Courses/Chapter 6 - Lists (Exercises).md b/Algo/Courses/Chapter 6 - Lists (Exercises).md index dc5b4bb..4c1029b 100644 --- a/Algo/Courses/Chapter 6 - Lists (Exercises).md +++ b/Algo/Courses/Chapter 6 - Lists (Exercises).md @@ -44,23 +44,17 @@ val search 'a -> 'a list -> bool = ## 1.4 - $n^{th}$ ```Ocaml -# let rec length l = - if l = [] then - 0 +# let nth l n = + if n <= 0 then + invalid_arg "n <= 0" else - let e::t = l in - 1 + length t;; -# let rec nth x n = - if (x = [] || n =< 0) then - Invalid_arg "The number of element in the list is 0" - else if length x < n then - Failwith "The list is too short" - else - let e::t = x in - if n = 0 - return e - else - nth t (n-1) ;; + let rec nthrec = function + | ([],_) -> failwith "list too short" + | (h::_,1)-> h + | (_::t, n) -> nthrec(t, n-1) + in + ntrec(l, n);; +val nth 'a list -> 'a -> int = ```