Skip to content

Commit f641feb

Browse files
committed
Lahenda crashcourse hulgad praktikumis
1 parent cd524e5 commit f641feb

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/crashcourse/collections.ml

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ let list_product (xs: int list): int =
6262

6363
(** Hulgad. *)
6464

65+
module Foo =
66+
struct
67+
type t = bool
68+
let x = 42
69+
let f y = 2 * y
70+
(* let f = fun y -> 2 * y *)
71+
end
72+
6573
(** Näited. *)
6674

6775
(** Täisarvude hulkade moodul. *)
@@ -71,7 +79,10 @@ module IntSet = Set.Make (Int)
7179
Vihje: IntSet.singleton.
7280
Vihje: IntSet.union. *)
7381
let rec intset_of_tree (t: int tree): IntSet.t =
74-
failwith "TODO"
82+
(* IntSet.of_list (list_of_tree t) *)
83+
match t with
84+
| Leaf x -> IntSet.singleton x
85+
| Branch (l, r) -> IntSet.union (intset_of_tree l) (intset_of_tree r)
7586

7687

7788
(** Puu elementide mooduli tüüp/signatuur. *)
@@ -98,10 +109,14 @@ struct
98109

99110
(** Teisendab puu hulgaks. *)
100111
let rec to_set (t: element tree): Set.t =
101-
failwith "TODO"
112+
match t with
113+
| Leaf x -> Set.singleton x
114+
| Branch (l, r) -> Set.union (to_set l) (to_set r)
102115

103116
(** Teisendab puu sõneks.
104117
Vihje: Element.show. *)
105118
let rec show (t: element tree): string =
106-
failwith "TODO"
119+
match t with
120+
| Leaf x -> Element.show x
121+
| Branch (l, r) -> "(" ^ show l ^ " " ^ show r ^ ")"
107122
end

0 commit comments

Comments
 (0)