Skip to content

Commit 9469c26

Browse files
committed
Lisa crashcourse
1 parent a1f66de commit 9469c26

File tree

12 files changed

+611
-0
lines changed

12 files changed

+611
-0
lines changed

src/crashcourse/basics.ml

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
(** Näited. *)
2+
3+
(** Lihtsad funktsioonid. *)
4+
5+
(** Suurendab täisarvu ühe võrra. *)
6+
let inc (x: int): int =
7+
failwith "TODO"
8+
9+
(** Suurendab täisarvu ühe võrra.
10+
Kasutada "lambdafunktsiooni". *)
11+
let inc': int -> int = fun x ->
12+
failwith "TODO"
13+
14+
15+
(** Rekursiivsed funktsioonid. *)
16+
17+
(** Arvutab faktoriaali.
18+
Kasutada if-i.
19+
Vihje: võrdusoperaator on = (mitte ==) ja selle vastand on <> (mitte !=). *)
20+
let rec fact (n: int): int =
21+
failwith "TODO"
22+
23+
(** OCaml-i if on avaldis, millel on väärtus, mitte lause: if a then b else c.
24+
Java-s jms. on selle analoog ternary operaator: a ? b : c. *)
25+
26+
(** Arvutab faktoriaali.
27+
Kasutada match-i. *)
28+
let rec fact' (n: int): int =
29+
failwith "TODO"
30+
31+
32+
(** Mitme argumendiga funktsioonid. *)
33+
34+
(** Arvutab suurima ühisteguri.
35+
Vt. https://en.wikipedia.org/wiki/Euclidean_algorithm#Implementations.
36+
Vihje: Jäägi operaator (mitte funktsioon) on mod. *)
37+
let rec gcd (x: int) (y: int): int =
38+
failwith "TODO"
39+
40+
(** Arvutab implikatsiooni.
41+
Kasutada mitme argumendiga match-i. *)
42+
let implies (x: bool) (y: bool): bool =
43+
failwith "TODO"
44+
45+
46+
47+
(** Ülesanded. *)
48+
49+
(** Suurendab täisarvu kolme võrra.
50+
Kasutada funktsiooni inc kolm korda järjest. *)
51+
let inc3 (x: int): int =
52+
failwith "TODO"
53+
54+
(** Arvutab kolmnurkarvu.
55+
Vt. https://en.wikipedia.org/wiki/Triangular_number.
56+
Kasutada rekursiooni, mitte valemit. *)
57+
let rec triangular (n: int): int =
58+
failwith "TODO"
59+
60+
(** Arvutab x astmes y.
61+
Võib eeldada, et y pole negatiivne.
62+
Kasutada rekursiooni, mitte ** operaatorit. *)
63+
let rec pow (x: int) (y: int): int =
64+
failwith "TODO"
65+
66+
(** Näide:
67+
Kas täht on täishäälik (inglise keeles)? *)
68+
let is_vowel (c: char): bool =
69+
c = 'a' || c = 'e' || c = 'i' || c = 'o' || c = 'u'
70+
71+
(** Kas täht on täishäälik (inglise keeles)?
72+
Kasutada match-i. *)
73+
let is_vowel' (c: char): bool =
74+
failwith "TODO"
75+
76+
(** Arvutab välistava või (xor).
77+
Kasutada mitme argumendiga match-i. *)
78+
let xor (x: bool) (y: bool): bool =
79+
failwith "TODO"

src/crashcourse/collections.ml

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
open Types
2+
3+
(** Listid. *)
4+
5+
(** Näited. *)
6+
7+
(** Teisendab puu elementide listiks (keskjärjestuses).
8+
Vihje: Listide konkateneerimise operaator on @. *)
9+
let rec list_of_tree (t: 'a tree): 'a list =
10+
failwith "TODO"
11+
12+
(** Suurendab täisarvude listi elemente ühe võrra. *)
13+
let list_inc (xs: int list): int list =
14+
failwith "TODO"
15+
16+
(** Jätab täisarvude listist alles elemendid, mis on vähemalt 4. *)
17+
let list_big (xs: int list): int list =
18+
failwith "TODO"
19+
20+
(** Liidab kokku täisarvude listi elemendid. *)
21+
let list_sum (xs: int list): int =
22+
failwith "TODO"
23+
24+
(** Liidab kokku täisarvude listi elemendid.
25+
Imperatiivsem lahendus. *)
26+
let list_sum' (xs: int list): int =
27+
let acc = ref 0 in
28+
List.iter (fun x ->
29+
acc := !acc + x
30+
) xs;
31+
!acc
32+
33+
(** Liidab kokku täisarvude listi elemendid.
34+
Rekursiivne funktsionaalne lahendus. *)
35+
let rec list_sum'' (xs: int list): int =
36+
match xs with
37+
| [] -> 0
38+
| x :: xs' -> x + list_sum'' xs'
39+
40+
41+
(** Ülesanded. *)
42+
43+
(** Korrutab täisarvude listi elemendid kahega. *)
44+
let list_double (xs: int list): int list =
45+
failwith "TODO"
46+
47+
(** Jätab täisarvude listist alles elemendid, mis on paaris. *)
48+
let list_even (xs: int list): int list =
49+
failwith "TODO"
50+
51+
(** Korrutab kokku täisarvude listi elemendid. *)
52+
let list_product (xs: int list): int =
53+
failwith "TODO"
54+
55+
56+
57+
(** Hulgad. *)
58+
59+
(** Näited. *)
60+
61+
(** Täisarvude hulkade moodul. *)
62+
module IntSet = Set.Make (Int)
63+
64+
(** Teisendab täisarvude puu hulgaks.
65+
Vihje: IntSet.singleton.
66+
Vihje: IntSet.union. *)
67+
let rec intset_of_tree (t: int tree): IntSet.t =
68+
failwith "TODO"
69+
70+
71+
(** Puu elementide mooduli tüüp/signatuur. *)
72+
module type TreeElement =
73+
sig
74+
(** Abstraktne elemendi tüüp. *)
75+
type t
76+
77+
(** Standardne võrdlusfunktsioon. *)
78+
val compare: t -> t -> int
79+
80+
(** Teisendab elemendi sõneks. *)
81+
val show: t -> string
82+
end
83+
84+
(** Puude moodul, mille argumendiks on elementide moodul.
85+
Parametriseeritud mooduleid nimetatakse funktoriteks. *)
86+
module Tree (Element: TreeElement) =
87+
struct
88+
type element = Element.t
89+
90+
(** Elementide hulkade moodul. *)
91+
module Set = Set.Make (Element)
92+
93+
(** Teisendab puu hulgaks. *)
94+
let rec to_set (t: element tree): Set.t =
95+
failwith "TODO"
96+
97+
(** Teisendab puu sõneks.
98+
Vihje: Element.show. *)
99+
let rec show (t: element tree): string =
100+
failwith "TODO"
101+
end

src/crashcourse/dune

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
(library
2+
(name crashcourse))

src/crashcourse/types.ml

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
(** Enum tüübid. *)
2+
3+
type color =
4+
| Red
5+
| Green
6+
| Blue
7+
8+
(** Näide:
9+
Teisendab värvi sõneks, vastavalt "R", "G" ja "B".
10+
Kasutada match-i. *)
11+
let show_color (c: color): string =
12+
failwith "TODO"
13+
14+
(** Ülesanne:
15+
Teisendab sõne värviks. show_color pöördfunktsioon.
16+
Kasutada match-i.
17+
Vihje: Mittesobiva juhu jaoks kasuta wildcard mustrit _.
18+
Vihje: Vea jaoks kasuta failwith funktsiooni. *)
19+
let parse_color (s: string): color =
20+
failwith "TODO"
21+
22+
23+
(** Algebralised andmetüübid. *)
24+
25+
(** Polümorfne kahendpuu, mille elemendid on tüüpi 'a. *)
26+
type 'a tree =
27+
| Leaf of 'a (** Leht sisaldab ühte väärtust. *)
28+
| Branch of 'a tree * 'a tree (** Vahetipul on kaks alampuud, väärtust pole. *)
29+
30+
(** Näide:
31+
.
32+
/ \
33+
1 .
34+
/ \
35+
. 5
36+
/ \
37+
. 4
38+
/ \
39+
2 3 *)
40+
let example_int_tree = Leaf 1
41+
42+
(** Ülesanne:
43+
.
44+
/ \
45+
. .
46+
/ \ / \
47+
'a' 'b' 'b' 'a' *)
48+
let example_char_tree = Leaf 'a'
49+
50+
51+
(** Näited. *)
52+
53+
(** Arvutab puu kõrguse. Lehe kõrgus on 0.
54+
Vihje: Kasuta max funktsiooni. *)
55+
let rec height (t: 'a tree): int =
56+
failwith "TODO"
57+
58+
(** Teisendab puu sõneks. Vt teste.
59+
Vihje: Sõnede konkateneerimise operaator on ^. *)
60+
let rec show_tree (show_leaf: 'a -> string) (t: 'a tree): string =
61+
failwith "TODO"
62+
63+
(** Rakendab funktsooni puu lehtedele. *)
64+
let rec tree_map (f: 'a -> 'b) (t: 'a tree): 'b tree =
65+
failwith "TODO"
66+
67+
68+
(** Ülesanded. *)
69+
70+
(** Arvutab lehtede arvu. *)
71+
let rec leaves (t: 'a tree): int =
72+
failwith "TODO"
73+
74+
(** Peegeldab puu, st. vahetab harud. *)
75+
let rec tree_mirror (t: 'a tree): 'a tree =
76+
failwith "TODO"
77+
78+
(** Leiab puu vasakpoolseima lehe väärtuse. *)
79+
let rec tree_left (t: 'a tree): 'a =
80+
failwith "TODO"

test/crashcourse/basics_test.ml

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
open OUnit2
2+
open Crashcourse.Basics
3+
4+
let test_inc _ =
5+
let assert_equal = assert_equal ~printer:string_of_int in
6+
assert_equal 5 (inc 4);
7+
assert_equal 1 (inc 0)
8+
9+
let test_inc' _ =
10+
let assert_equal = assert_equal ~printer:string_of_int in
11+
assert_equal 5 (inc' 4);
12+
assert_equal 1 (inc' 0)
13+
14+
let test_fact _ =
15+
let assert_equal = assert_equal ~printer:string_of_int in
16+
assert_equal 1 (fact 0);
17+
assert_equal 1 (fact 1);
18+
assert_equal 2 (fact 2);
19+
assert_equal 6 (fact 3);
20+
assert_equal 24 (fact 4);
21+
assert_equal 120 (fact 5)
22+
23+
let test_fact' _ =
24+
let assert_equal = assert_equal ~printer:string_of_int in
25+
assert_equal 1 (fact' 0);
26+
assert_equal 1 (fact' 1);
27+
assert_equal 2 (fact' 2);
28+
assert_equal 6 (fact' 3);
29+
assert_equal 24 (fact' 4);
30+
assert_equal 120 (fact' 5)
31+
32+
let test_gcd _ =
33+
let assert_equal = assert_equal ~printer:string_of_int in
34+
assert_equal 1 (gcd 3 5);
35+
assert_equal 3 (gcd 3 9);
36+
assert_equal 3 (gcd 9 6);
37+
assert_equal 6 (gcd 12 18)
38+
39+
let test_implies _ =
40+
let assert_equal = assert_equal ~printer:string_of_bool in
41+
assert_equal true (implies false false);
42+
assert_equal true (implies false true);
43+
assert_equal false (implies true false);
44+
assert_equal true (implies true true)
45+
46+
let test_inc3 _ =
47+
let assert_equal = assert_equal ~printer:string_of_int in
48+
assert_equal 7 (inc3 4);
49+
assert_equal 3 (inc3 0)
50+
51+
let test_triangular _ =
52+
let assert_equal = assert_equal ~printer:string_of_int in
53+
assert_equal 0 (triangular 0);
54+
assert_equal 1 (triangular 1);
55+
assert_equal 3 (triangular 2);
56+
assert_equal 6 (triangular 3);
57+
assert_equal 10 (triangular 4)
58+
59+
let test_pow _ =
60+
let assert_equal = assert_equal ~printer:string_of_int in
61+
assert_equal 1 (pow 2 0);
62+
assert_equal 2 (pow 2 1);
63+
assert_equal 4 (pow 2 2);
64+
assert_equal 8 (pow 2 3);
65+
assert_equal 16 (pow 2 4);
66+
assert_equal 9 (pow 3 2)
67+
68+
let test_is_vowel _ =
69+
let assert_equal = assert_equal ~printer:string_of_bool in
70+
assert_equal true (is_vowel 'a');
71+
assert_equal true (is_vowel 'e');
72+
assert_equal true (is_vowel 'i');
73+
assert_equal true (is_vowel 'o');
74+
assert_equal true (is_vowel 'u');
75+
assert_equal false (is_vowel 'x');
76+
assert_equal false (is_vowel 'm');
77+
assert_equal false (is_vowel 'b')
78+
79+
let test_is_vowel' _ =
80+
let assert_equal = assert_equal ~printer:string_of_bool in
81+
assert_equal true (is_vowel' 'a');
82+
assert_equal true (is_vowel' 'e');
83+
assert_equal true (is_vowel' 'i');
84+
assert_equal true (is_vowel' 'o');
85+
assert_equal true (is_vowel' 'u');
86+
assert_equal false (is_vowel' 'x');
87+
assert_equal false (is_vowel' 'm');
88+
assert_equal false (is_vowel' 'b')
89+
90+
let test_xor _ =
91+
let assert_equal = assert_equal ~printer:string_of_bool in
92+
assert_equal false (xor false false);
93+
assert_equal true (xor false true);
94+
assert_equal true (xor true false);
95+
assert_equal false (xor true true)
96+
97+
let tests =
98+
"basics" >::: [
99+
"examples" >::: [
100+
"inc" >:: test_inc;
101+
"inc'" >:: test_inc';
102+
"fact" >:: test_fact;
103+
"fact'" >:: test_fact';
104+
"gcd" >:: test_gcd;
105+
"implies" >:: test_implies;
106+
];
107+
"problems" >::: [
108+
"inc3" >:: test_inc3;
109+
"triangular" >:: test_triangular;
110+
"pow" >:: test_pow;
111+
"is_vowel" >:: test_is_vowel;
112+
"is_vowel'" >:: test_is_vowel';
113+
"xor" >:: test_xor;
114+
];
115+
]

test/crashcourse/basics_test.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
val tests: OUnit2.test

0 commit comments

Comments
 (0)