Solve up to problem 3-4 of HW1

Signed-off-by: jmug <u.g.a.mariano@gmail.com>
This commit is contained in:
Mariano Uvalle 2025-01-26 21:47:38 -08:00
parent 42aeacd9c1
commit 226956072a
2 changed files with 28 additions and 12 deletions

View file

@ -350,10 +350,10 @@ let quadrupled_z_again : int = twice double z (* pass double to twice *)
makes the first case of part1_tests "Problem 1" succeed. See the
gradedtests.ml file.
*)
let pieces : int = -1
let pieces : int = 8
(* Implement a function cube that takes an int value and produces its cube. *)
let cube : int -> int = fun _ -> failwith "cube unimplemented"
let cube : int -> int = fun x -> x * x * x
(* Problem 1-2 *)
(*
@ -366,7 +366,7 @@ let cube : int -> int = fun _ -> failwith "cube unimplemented"
and computes the total value of the coins in cents:
*)
let cents_of : int -> int -> int -> int -> int =
fun _ -> failwith "cents_of unimplemented"
fun q d n p -> p + (n * 5) + (d * 10) + (q * 25)
(* Problem 1-3 *)
(*
@ -508,7 +508,7 @@ let pair_up (x : 'a) : 'a * 'a = (x, x)
Complete the definition of third_of_three; be sure to give it
the correct type signature (we will grade that part manually):
*)
let third_of_three _ = failwith "third_of_three unimplemented"
let third_of_three (t : 'a * 'b * 'c) : 'c = match t with _, _, x -> x
(*
Problem 2-2
@ -520,7 +520,9 @@ let third_of_three _ = failwith "third_of_three unimplemented"
*)
let compose_pair (p : ('b -> 'c) * ('a -> 'b)) : 'a -> 'c =
failwith "compose_pair unimplemented"
match p with
f1, f2 -> fun (x : 'a) -> f1 (f2 x)
(******************************************************************************)
(* *)
@ -673,7 +675,9 @@ let rec mylist_to_list (l : 'a mylist) : 'a list =
the inverse of the mylist_to_list function given above.
*)
let rec list_to_mylist (l : 'a list) : 'a mylist =
failwith "list_to_mylist unimplemented"
match l with
| [] -> Nil
| x :: xs -> Cons (x, list_to_mylist xs)
(*
Problem 3-2
@ -689,7 +693,12 @@ let rec list_to_mylist (l : 'a list) : 'a mylist =
append. So (List.append [1;2] [3]) is the same as ([1;2] @ [3]).
*)
let rec append (l1 : 'a list) (l2 : 'a list) : 'a list =
failwith "append unimplemented"
match l1 with
(* Matching on the non empty case first to avoid having to use parens for the nested match *)
| x :: xs -> x :: append xs l2
| [] -> match l2 with
| x :: xs -> x :: append [] xs
| [] -> []
(*
Problem 3-3
@ -698,7 +707,9 @@ let rec append (l1 : 'a list) (l2 : 'a list) : 'a list =
you might want to call append. Do not use the library function.
*)
let rec rev (l : 'a list) : 'a list =
failwith "rev unimplemented"
match l with
| [] -> []
| x :: xs -> append (rev xs) [x]
(*
Problem 3-4
@ -710,7 +721,12 @@ let rec rev (l : 'a list) : 'a list =
OCaml will compile a tail recursive function to a simple loop.
*)
let rev_t (l : 'a list) : 'a list =
failwith "rev_t unimplemented"
let rec rev_t' (l' : 'a list) (acc : 'a list) : 'a list =
match l' with
| [] -> acc
| x :: xs -> rev_t' xs (x :: acc)
in
rev_t' l []
(*
Problem 3-5

View file

@ -8,9 +8,9 @@ open Hellocaml
let student_tests : suite = [
Test ("Student-Provided Tests For Problem 1-3", [
("case1", assert_eqf (fun () -> failwith "Problem 3 case1 test unimplemented") prob3_ans );
("case2", assert_eqf (fun () -> failwith "Problem 3 case2 test unimplemented") (prob3_case2 17) );
("case3", assert_eqf (fun () -> prob3_case3) 0);
("case1", assert_eqf (fun () -> prob3_ans) 42);
("case2", assert_eqf (fun () -> (prob3_case2 17)) 25);
("case3", assert_eqf (fun () -> prob3_case3) 64);
]);
]