1 | open Printf |
---|
2 | |
---|
3 | type s = { i : int; j : int } |
---|
4 | let sv : s = { i = 3; j = 5 } |
---|
5 | type adt = |
---|
6 | I of int | |
---|
7 | F of float | |
---|
8 | S of s |
---|
9 | let adtprt( adtv : adt ) = |
---|
10 | match adtv with (* pattern matching *) |
---|
11 | I i -> printf "%d\n" i | |
---|
12 | F f -> printf "%g\n" f | |
---|
13 | S sv -> printf "%d %d\n" sv.i sv.j |
---|
14 | |
---|
15 | let silly( adtv : adt ) = |
---|
16 | if adtv <= F(3.5) then |
---|
17 | printf "<= F\n" |
---|
18 | else if adtv >= S(sv) then |
---|
19 | printf ">= S\n" |
---|
20 | |
---|
21 | let adtv : adt = I(3) let _ = adtprt( adtv ); silly( adtv ) |
---|
22 | let adtv : adt = F(3.5) let _ = adtprt( adtv ); silly( adtv ) |
---|
23 | let adtv : adt = S(sv) let _ = adtprt( adtv ); silly( adtv ) |
---|
24 | |
---|
25 | type week = Mon | Tue | Wed | Thu | Fri | Sat | Sun [@@deriving enumerate] |
---|
26 | let _ = List.iter ( fun e -> printf "%d" (to_val e) ) all_of_week |
---|
27 | |
---|
28 | let day : week = Mon |
---|
29 | |
---|
30 | let take_class( d : week ) = |
---|
31 | if d <= Fri then |
---|
32 | printf "weekday\n" |
---|
33 | else if d >= Sat then |
---|
34 | printf "weekend\n"; |
---|
35 | match d with |
---|
36 | Mon | Wed -> printf "CS442\n" | |
---|
37 | Tue | Thu -> printf "CS343\n" | |
---|
38 | Fri -> printf "Tutorial\n" | |
---|
39 | _ -> printf "Take a break\n" |
---|
40 | |
---|
41 | let _ = take_class( Mon ); take_class( Sat ); |
---|
42 | |
---|
43 | type weekday = Mon | Tue | Wed | Thu | Fri |
---|
44 | type weekend = Sat | Sun of float |
---|
45 | type week = Weekday of weekday | Weekend of weekend |
---|
46 | let day : week = Weekend (Sun 3.5) |
---|
47 | |
---|
48 | let take_class( d : week ) = |
---|
49 | if d <= Weekday Fri then |
---|
50 | printf "weekday\n" |
---|
51 | else if d >= Weekend Sat then |
---|
52 | printf "weekend\n"; |
---|
53 | match d with |
---|
54 | Weekday Mon | Weekday Wed -> printf "CS442\n" | |
---|
55 | Weekday Tue | Weekday Thu -> printf "CS343\n" | |
---|
56 | Weekday Fri -> printf "Tutorial\n" | |
---|
57 | _ -> printf "Take a break\n" |
---|
58 | let _ = take_class( day ) |
---|
59 | |
---|
60 | let _ = for i = 1 to 10 do |
---|
61 | printf "%d, " i |
---|
62 | done |
---|
63 | |
---|
64 | (* Local Variables: *) |
---|
65 | (* tab-width: 4 *) |
---|
66 | (* compile-command: "ocaml test.ml" *) |
---|
67 | (* End: *) |
---|