restructure + lecutre 07
This commit is contained in:
96 files changed
+458
-4
No files matched your search
+114
@@ -0,0 +1,114 @@
|
||||
(** [fact n] calculates the factorial of [n]; non tail recursive *)
|
||||
let rec fact n =
|
||||
if n = 0. then 1.
|
||||
else n *. fact(n -. 1.);;
|
||||
|
||||
(**/**)
|
||||
let test_fact () =
|
||||
assert (fact 0. = 1.);
|
||||
assert (fact 3. = 6.);
|
||||
assert (fact 5. = 120.)
|
||||
(**/**)
|
||||
|
||||
(** [fact_tr n] calculates the factorial of [n]; tail recursive *)
|
||||
let fact_tr n =
|
||||
let rec fact_tr' acc i =
|
||||
if i = 0. then acc
|
||||
else fact_tr' (i *. acc) (i -. 1.)
|
||||
in
|
||||
fact_tr' 1. n;;
|
||||
|
||||
(**/**)
|
||||
let test_fact_tr () =
|
||||
assert (fact_tr 0. = 1.);
|
||||
assert (fact_tr 3. = 6.);
|
||||
assert (fact_tr 5. = 120.)
|
||||
(**/**)
|
||||
|
||||
(** [pow_tr a b] calculates [a] to the power of [b]; non tail recursive *)
|
||||
let rec pow a b =
|
||||
if b = 0. then 1.
|
||||
else a *. pow a (b -. 1.);;
|
||||
|
||||
(**/**)
|
||||
let test_pow () =
|
||||
assert (pow 0. 1. = 0.);
|
||||
assert (pow 0. 5. = 0.);
|
||||
assert (pow 1. 0. = 1.);
|
||||
assert (pow 5. 0. = 1.);
|
||||
assert (pow 1. 1. = 1.);
|
||||
assert (pow 5. 1. = 5.);
|
||||
assert (pow 2. 3. = 8.)
|
||||
(**/**)
|
||||
|
||||
(** [pow_tr a b] calculates [a] to the power of [b]; tail recursive *)
|
||||
let pow_tr a b =
|
||||
let rec pow_tr' acc i =
|
||||
if i = 0. then acc
|
||||
else pow_tr' (acc *. a) (i -. 1.)
|
||||
in
|
||||
pow_tr' 1. b;;
|
||||
|
||||
(**/**)
|
||||
let test_pow_tr () =
|
||||
assert (pow_tr 0. 1. = 0.);
|
||||
assert (pow_tr 0. 5. = 0.);
|
||||
assert (pow_tr 1. 0. = 1.);
|
||||
assert (pow_tr 5. 0. = 1.);
|
||||
assert (pow_tr 1. 1. = 1.);
|
||||
assert (pow_tr 5. 1. = 5.);
|
||||
assert (pow_tr 2. 3. = 8.)
|
||||
(**/**)
|
||||
|
||||
(** [expo_tr n x] calculates the approximation of e to the power of [x];
|
||||
* with a max iteration detail of [n]; non tail recursive
|
||||
* Require: [n] >= 1
|
||||
*)
|
||||
let rec expo n x =
|
||||
if n = 0 then 1.
|
||||
else pow x (float_of_int n) /. fact (float_of_int n) +.
|
||||
expo (n - 1) x;;
|
||||
|
||||
(**/**)
|
||||
let test_expo () =
|
||||
let tolerance = 1e-6 in
|
||||
let diff = abs_float (expo 20 1. -. exp 1.) in
|
||||
assert (diff < tolerance);
|
||||
let diff = abs_float (expo 20 2. -. exp 2.) in
|
||||
assert (diff < tolerance);
|
||||
let diff = abs_float (expo 20 3. -. exp 3.) in
|
||||
assert (diff < tolerance)
|
||||
(**/**)
|
||||
|
||||
(** [expo_tr n x] calculates the approximation of e to the power of [x];
|
||||
* with a max iteration detail of [n]; tail recursive
|
||||
* Require: [n] >= 1
|
||||
*)
|
||||
let expo_tr n x =
|
||||
let rec expo_tr' acc i =
|
||||
if i = 0 then acc
|
||||
else expo_tr' (acc +. pow_tr x (float_of_int i) /.
|
||||
fact_tr (float_of_int i)) (i - 1)
|
||||
in
|
||||
expo_tr' 1. n;;
|
||||
|
||||
(**/**)
|
||||
let test_expo_tr () =
|
||||
let tolerance = 1e-6 in
|
||||
let diff = abs_float (expo_tr 20 1. -. exp 1.) in
|
||||
assert (diff < tolerance);
|
||||
let diff = abs_float (expo_tr 20 2. -. exp 2.) in
|
||||
assert (diff < tolerance);
|
||||
let diff = abs_float (expo_tr 20 3. -. exp 3.) in
|
||||
assert (diff < tolerance)
|
||||
(**/**)
|
||||
|
||||
(**/**)
|
||||
let run_all_tests () =
|
||||
test_fact();
|
||||
test_fact_tr();
|
||||
test_pow();
|
||||
test_pow_tr();
|
||||
test_expo();
|
||||
test_expo_tr()
|
||||
(**/**)
|
||||
+155
@@ -0,0 +1,155 @@
|
||||
(** [reverse l] returns the reverse order of list [l]; non tail recursive *)
|
||||
let rec reverse l =
|
||||
match l with
|
||||
| [] -> []
|
||||
| x :: xs -> reverse xs @ [x];;
|
||||
|
||||
(**/**)
|
||||
let test_reverse () =
|
||||
assert (reverse [] = []);
|
||||
assert (reverse [1] = [1]);
|
||||
assert (reverse [1; 2] = [2; 1])
|
||||
(**/**)
|
||||
|
||||
(** [reverse_tr l] returns the reverse order of list [l]; tail recursive *)
|
||||
let reverse_tr l =
|
||||
let rec reverse_tr' acc l =
|
||||
match l with
|
||||
| [] -> acc
|
||||
| x :: xs -> reverse_tr' (x :: acc) xs
|
||||
in
|
||||
reverse_tr' [] l;;
|
||||
|
||||
(**/**)
|
||||
let test_reverse_tr () =
|
||||
assert (reverse_tr [] = []);
|
||||
assert (reverse_tr [1] = [1]);
|
||||
assert (reverse_tr [1; 2] = [2; 1])
|
||||
(**/**)
|
||||
|
||||
(** [zip l1 l2] combines elements from [l1] and [l2] into a
|
||||
* new list of tuples; non tail recursive
|
||||
*)
|
||||
let rec zip l1 l2 =
|
||||
match l1, l2 with
|
||||
| [], _ | _, [] -> [] (* if l1 or l2 is empty, return empty *)
|
||||
| x1 :: xs1, x2 :: xs2 ->
|
||||
(x1, x2) :: zip xs1 xs2;;
|
||||
|
||||
(**/**)
|
||||
let test_zip () =
|
||||
assert (zip [] [] = []);
|
||||
assert (zip [1] [] = []);
|
||||
assert (zip [] ['a'] = []);
|
||||
assert (zip [1; 2; 3] ['a'; 'b'] = [(1, 'a'); (2, 'b')]);
|
||||
assert (zip [1; 2; 3] ['a'; 'b'; 'c'] = [(1, 'a'); (2, 'b'); (3, 'c')])
|
||||
(**/**)
|
||||
|
||||
(** [zip_tr l1 l2] combines elements from [l1] and [l2] into a
|
||||
* new list of tuples; tail recursive
|
||||
*)
|
||||
let zip_tr l1 l2 =
|
||||
let rec zip_tr' acc l1 l2 =
|
||||
match l1, l2 with
|
||||
| [], _ | _, [] -> reverse_tr acc (* if l1 or l2 is empty, return empty *)
|
||||
| x1 :: xs1, x2 :: xs2 ->
|
||||
zip_tr' ((x1, x2) :: acc) xs1 xs2
|
||||
in
|
||||
zip_tr' [] l1 l2;;
|
||||
|
||||
(**/**)
|
||||
let test_zip_tr () =
|
||||
assert (zip_tr [] [] = []);
|
||||
assert (zip_tr [1] [] = []);
|
||||
assert (zip_tr [] ['a'] = []);
|
||||
assert (zip_tr [1; 2; 3] ['a'; 'b'] = [(1, 'a'); (2, 'b')]);
|
||||
assert (zip_tr [1; 2; 3] ['a'; 'b'; 'c'] = [(1, 'a'); (2, 'b'); (3, 'c')])
|
||||
(**/**)
|
||||
|
||||
(** [unzip l] takes in a list of tuples [l] where each tuple is
|
||||
* a pair, we seperate the pairs (x, y) into sepeate lists, ([x], [y])
|
||||
* and return a tuple of both lists; non tail recursive *)
|
||||
let rec unzip l =
|
||||
match l with
|
||||
| [] -> ([], [])
|
||||
| (x, y) :: xys ->
|
||||
let (l1, l2) = unzip xys in
|
||||
x :: l1, y :: l2;;
|
||||
|
||||
(**/**)
|
||||
let test_unzip () =
|
||||
assert (unzip [] = ([], []));
|
||||
assert (unzip [(1, 'a')] = ([1], ['a']));
|
||||
assert (unzip [(1, 'a'); (2, 'b')] = ([1; 2], ['a'; 'b']))
|
||||
(**/**)
|
||||
|
||||
(** [unzip_tr l] takes in a list of tuples [l] where each tuple is
|
||||
* a pair, we seperate the pairs (x, y) into sepeate lists, ([x], [y])
|
||||
* and return a tuple of both lists; tail recursive *)
|
||||
let unzip_tr l =
|
||||
let rec unzip_tr' (a1, a2) l =
|
||||
match l with
|
||||
| [] -> (a1, a2)
|
||||
| (x, y) :: xys ->
|
||||
unzip_tr' (x :: a1, y :: a2) xys
|
||||
in
|
||||
unzip_tr' ([], []) (reverse_tr l);;
|
||||
|
||||
(**/**)
|
||||
let test_unzip_tr () =
|
||||
assert (unzip_tr [] = ([], []));
|
||||
assert (unzip_tr [(1, 'a')] = ([1], ['a']));
|
||||
assert (unzip_tr [(1, 'a'); (2, 'b')] = ([1; 2], ['a'; 'b']))
|
||||
(**/**)
|
||||
|
||||
(** [dedup l] takes in a list [l] and collapses consecutive duplicated
|
||||
* elements into a single element; non tail recursive *)
|
||||
let rec dedup l =
|
||||
match l with
|
||||
| [] -> []
|
||||
| [x] -> l
|
||||
| x :: y :: zs ->
|
||||
if x = y then dedup (x :: zs)
|
||||
else x :: dedup (y :: zs);;
|
||||
|
||||
(**/**)
|
||||
let test_dedup () =
|
||||
assert (dedup [] = []);
|
||||
assert (dedup [1] = [1]);
|
||||
assert (dedup [1; 2] = [1; 2]);
|
||||
assert (dedup [1; 1; 2; 2; 2; 1; 3; 3; 2] = [1; 2; 1; 3; 2]);
|
||||
assert (dedup [1; 1; 2; 2; 2; 1; 3; 3; 2; 4] = [1; 2; 1; 3; 2; 4])
|
||||
(**/**)
|
||||
|
||||
(** [dedup l] takes in a list [l] and collapses consecutive duplicated
|
||||
* elements into a single element; tail recursive *)
|
||||
let dedup_tr l =
|
||||
let rec dedup' acc l =
|
||||
match l with
|
||||
| [] -> acc
|
||||
| [x] -> reverse_tr (x :: acc)
|
||||
| x :: y :: zs ->
|
||||
if x = y then dedup' acc (x :: zs)
|
||||
else dedup' (x :: acc) (y :: zs)
|
||||
in
|
||||
dedup' [] l;;
|
||||
|
||||
(**/**)
|
||||
let test_dedup_tr () =
|
||||
assert (dedup_tr [] = []);
|
||||
assert (dedup_tr [1] = [1]);
|
||||
assert (dedup_tr [1; 2] = [1; 2]);
|
||||
assert (dedup_tr [1; 1; 2; 2; 2; 1; 3; 3; 2] = [1; 2; 1; 3; 2])
|
||||
(**/**)
|
||||
|
||||
(**/**)
|
||||
let run_all_tests () =
|
||||
test_reverse();
|
||||
test_reverse_tr();
|
||||
test_zip();
|
||||
test_zip_tr();
|
||||
test_unzip();
|
||||
test_unzip_tr();
|
||||
test_dedup();
|
||||
test_dedup_tr();
|
||||
(**/**)
|
||||
Reference in new issue
Block a user