Skip to content

Commit

Permalink
Solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
Cuihtlauac ALVARADO committed Jan 17, 2024
1 parent 5089f82 commit b2b2e69
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 1 deletion.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
### Docs

```shell
# dune build @docs

# open _build/default/_doc/_html/index.html
```

### Tests

```
# dune test
```

5 changes: 4 additions & 1 deletion dune
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
(library (name camellito) (public_name camellito))
(library (name camellito) (wrapped false) (public_name camellito) (modules task))

(test (name test) (libraries camellito ounit2) (modules test))

10 changes: 10 additions & 0 deletions task.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
let ignore _ = ()

let map f u =
let rec loop acc = function
| [] -> acc
| x :: u -> loop (f x :: acc) u in
List.rev (loop [] u)

let range lo =
let rec loop u i = if i < lo then u else loop (i :: u) (i - 1) in loop []
24 changes: 24 additions & 0 deletions task.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
(** Module [Tasks] contains the functions that need to be implemented.
The file [task.mli] contains the signature and description of the functions.
It should not be modiled.
Write your functions in the file [task.ml]. Replace the dummy content
[failwith "Not yet implemented"] by your code.
The [[@warning "-27"]] pragma can be removed once a function is implemented.
*)

val ignore : 'a -> unit
(** Discards the value of its argument and returns the unit value [()]. For
instance, [ignore (f x)] discards the result of the function [f]. Side effects
triggered by the evaluation of the argument are unchanged. *)

val map : ('a -> 'b) -> 'a list -> 'b list
(** [map f [a1; ...; an]] applies function [f] to [[a1, ..., an]], and builds
the list [[f a1; ...; f an]] with the results returned by [f].
*)

val range : int -> int -> int list
(** [range lo hi] returns a list of integers [[lo; lo + 1; ...; hi]]. It
returns an empty list if [hi < lo]. This function is tail-recursive. *)
28 changes: 28 additions & 0 deletions test.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
open OUnit2

let sq x = x * x

let ignore = [
"unit" >:: (fun _ -> assert_equal () (Task.ignore ()));
"42" >:: (fun _ -> assert_equal () (Task.ignore 42));
"apply" >:: (fun _ -> assert_equal () (Task.ignore (sq 3)));
"print" >:: (fun _ -> assert_equal () (Task.ignore (print_newline ())));
]

let map = [
"nil" >:: (fun _ -> assert_equal [] (Task.map sq []));
"sq" >:: (fun _ -> assert_equal [1; 4; 9] (Task.map sq [1; 2; 3]));
"id" >:: (fun _ -> assert_equal [1; 2; 3] (Task.map Fun.id [1; 2; 3]));
"then" >:: (fun _ -> assert_equal ([1; 2; 3] |> Task.map (( + ) 1) |> Task.map sq) (Task.map (fun x -> x |> ( + ) 1 |> sq) [1; 2; 3]));
]

let range = [
"nil" >:: (fun _ -> assert_equal [] (Task.range 7 3));
"cons" >:: (fun _ -> assert_equal [3; 4; 5] (Task.range 3 5));
]

let () = "Task" >::: [
"ignore" >::: ignore;
"map" >::: map;
"range" >::: range
] |> run_test_tt_main

0 comments on commit b2b2e69

Please sign in to comment.