Skip to content

Commit cd524e5

Browse files
committed
Lahenda crashcourse praktikumis
1 parent 9469c26 commit cd524e5

File tree

3 files changed

+75
-17
lines changed

3 files changed

+75
-17
lines changed

src/crashcourse/basics.ml

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
(** Suurendab täisarvu ühe võrra. *)
66
let inc (x: int): int =
7-
failwith "TODO"
7+
x + 1
88

99
(** Suurendab täisarvu ühe võrra.
1010
Kasutada "lambdafunktsiooni". *)
11-
let inc': int -> int = fun x ->
12-
failwith "TODO"
11+
let inc': int -> int =
12+
fun x -> x + 1
1313

1414

1515
(** Rekursiivsed funktsioonid. *)
@@ -18,15 +18,20 @@ let inc': int -> int = fun x ->
1818
Kasutada if-i.
1919
Vihje: võrdusoperaator on = (mitte ==) ja selle vastand on <> (mitte !=). *)
2020
let rec fact (n: int): int =
21-
failwith "TODO"
21+
if n = 0 then
22+
1
23+
else
24+
n * fact (n - 1)
2225

2326
(** OCaml-i if on avaldis, millel on väärtus, mitte lause: if a then b else c.
2427
Java-s jms. on selle analoog ternary operaator: a ? b : c. *)
2528

2629
(** Arvutab faktoriaali.
2730
Kasutada match-i. *)
2831
let rec fact' (n: int): int =
29-
failwith "TODO"
32+
match n with
33+
| 0 -> 1
34+
| _ -> n * fact' (n - 1)
3035

3136

3237
(** Mitme argumendiga funktsioonid. *)
@@ -35,12 +40,23 @@ let rec fact' (n: int): int =
3540
Vt. https://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations.
3641
Vihje: Jäägi operaator (mitte funktsioon) on mod. *)
3742
let rec gcd (x: int) (y: int): int =
38-
failwith "TODO"
43+
if y = 0 then
44+
x
45+
else
46+
gcd y (x mod y)
3947

4048
(** Arvutab implikatsiooni.
4149
Kasutada mitme argumendiga match-i. *)
4250
let implies (x: bool) (y: bool): bool =
43-
failwith "TODO"
51+
match x, y with
52+
(* | true, true -> true
53+
| true, false -> false
54+
| false, true -> true
55+
| false, false -> true *)
56+
(* | false, _ -> true
57+
| true, _ -> y *)
58+
| true, false -> false
59+
| _, _ -> true
4460

4561

4662

src/crashcourse/collections.ml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,25 @@ open Types
77
(** Teisendab puu elementide listiks (keskjärjestuses).
88
Vihje: Listide konkateneerimise operaator on @. *)
99
let rec list_of_tree (t: 'a tree): 'a list =
10-
failwith "TODO"
10+
match t with
11+
| Leaf x -> [x]
12+
| Branch (l, r) -> list_of_tree l @ list_of_tree r
1113

1214
(** Suurendab täisarvude listi elemente ühe võrra. *)
1315
let list_inc (xs: int list): int list =
14-
failwith "TODO"
16+
(* List.map (fun x -> x + 1) xs *)
17+
List.map Basics.inc xs
1518

1619
(** Jätab täisarvude listist alles elemendid, mis on vähemalt 4. *)
1720
let list_big (xs: int list): int list =
18-
failwith "TODO"
21+
List.filter (fun x -> x >= 4) xs
1922

2023
(** Liidab kokku täisarvude listi elemendid. *)
2124
let list_sum (xs: int list): int =
22-
failwith "TODO"
25+
(* List.fold_left (fun acc x ->
26+
acc + x
27+
) 0 xs *)
28+
List.fold_left (+) 0 xs
2329

2430
(** Liidab kokku täisarvude listi elemendid.
2531
Imperatiivsem lahendus. *)

src/crashcourse/types.ml

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ type color =
99
Teisendab värvi sõneks, vastavalt "R", "G" ja "B".
1010
Kasutada match-i. *)
1111
let show_color (c: color): string =
12-
failwith "TODO"
12+
match c with
13+
| Red -> "R"
14+
| Green -> "G"
15+
| Blue -> "B"
1316

1417
(** Ülesanne:
1518
Teisendab sõne värviks. show_color pöördfunktsioon.
@@ -22,6 +25,10 @@ let parse_color (s: string): color =
2225

2326
(** Algebralised andmetüübid. *)
2427

28+
(* type inttree =
29+
| Leaf of int (** Leht sisaldab ühte väärtust. *)
30+
| Branch of inttree * inttree (** Vahetipul on kaks alampuud, väärtust pole. *) *)
31+
2532
(** Polümorfne kahendpuu, mille elemendid on tüüpi 'a. *)
2633
type 'a tree =
2734
| Leaf of 'a (** Leht sisaldab ühte väärtust. *)
@@ -37,32 +44,61 @@ type 'a tree =
3744
. 4
3845
/ \
3946
2 3 *)
40-
let example_int_tree = Leaf 1
47+
let example_int_tree =
48+
Branch (
49+
Leaf 1,
50+
Branch (
51+
Branch (
52+
Branch (
53+
Leaf 2,
54+
Leaf 3
55+
),
56+
Leaf 4
57+
),
58+
Leaf 5
59+
)
60+
)
4161

4262
(** Ülesanne:
4363
.
4464
/ \
4565
. .
4666
/ \ / \
4767
'a' 'b' 'b' 'a' *)
48-
let example_char_tree = Leaf 'a'
68+
let example_char_tree =
69+
Branch (
70+
Branch (
71+
Leaf 'a',
72+
Leaf 'b'
73+
),
74+
Branch (
75+
Leaf 'b',
76+
Leaf 'a'
77+
)
78+
)
4979

5080

5181
(** Näited. *)
5282

5383
(** Arvutab puu kõrguse. Lehe kõrgus on 0.
5484
Vihje: Kasuta max funktsiooni. *)
5585
let rec height (t: 'a tree): int =
56-
failwith "TODO"
86+
match t with
87+
| Leaf _ -> 0
88+
| Branch (l, r) -> 1 + max (height l) (height r)
5789

5890
(** Teisendab puu sõneks. Vt teste.
5991
Vihje: Sõnede konkateneerimise operaator on ^. *)
6092
let rec show_tree (show_leaf: 'a -> string) (t: 'a tree): string =
61-
failwith "TODO"
93+
match t with
94+
| Leaf x -> show_leaf x
95+
| Branch (l, r) -> "(" ^ show_tree show_leaf l ^ " " ^ show_tree show_leaf r ^ ")"
6296

6397
(** Rakendab funktsooni puu lehtedele. *)
6498
let rec tree_map (f: 'a -> 'b) (t: 'a tree): 'b tree =
65-
failwith "TODO"
99+
match t with
100+
| Leaf x -> Leaf (f x)
101+
| Branch (l, r) -> Branch (tree_map f l, tree_map f r)
66102

67103

68104
(** Ülesanded. *)

0 commit comments

Comments
 (0)