source: doc/theses/jiada_liang_MMath/test.ml @ 1725989

Last change on this file since 1725989 was 1725989, checked in by Peter A. Buhr <pabuhr@…>, 2 weeks ago

add enumeration test programs for different programming languages

  • Property mode set to 100644
File size: 1.4 KB
Line 
1type s = { i :  int; }
2type fred = I of int | D of float | S of s
3type mary = I of int | D of int | S of int
4type weekday = Mon | Tue | Wed | Thu | Fri | Sat | Sun
5let day : weekday = Mon
6let take_class( d : weekday ) =
7        if d <= Fri then                                (* Gregor *)
8                Printf.printf "weekday\n"
9        else if d >= Sat then                   (* Gregor *)
10                Printf.printf "weekend\n";
11        match d with
12                Mon | Wed -> Printf.printf "CS442\n" |
13                Tue | Thu -> Printf.printf "CS343\n" |
14                Fri -> Printf.printf "Tutorial\n" |
15                _ -> Printf.printf "Take a break\n"
16
17let _ = take_class( Mon ); take_class( Sat );
18
19type colour = Red | Green of string | Blue of int * float
20let c = Red
21let _ = match c with Red -> Printf.printf "Red, "
22let c = Green( "abc" )
23let _ = match c with Green g -> Printf.printf "%s, " g
24let c = Blue( 1, 1.5 )
25let _ = match c with Blue( i, f ) -> Printf.printf "%d %g\n" i f
26
27let check_colour(c: colour): string =
28        if c < Green( "xyz" ) then              (* Gregor *)
29                Printf.printf "green\n";
30        match c with
31                Red -> "Red" |
32                Green g -> g |
33                Blue(i, f) -> string_of_int i ^ string_of_float f
34let _ = check_colour( Red ); check_colour( Green( "xyz" ) );
35
36type stringList = Empty | Pair of string * stringList
37let rec len_of_string_list(l: stringList): int =
38        match l with
39                Empty -> 0 |
40                Pair(_ , r) -> 1 + len_of_string_list r
41
42let _ = for i = 1 to 10 do
43        Printf.printf "%d, " i
44done
45
46(* Local Variables: *)
47(* tab-width: 4 *)
48(* compile-command: "ocaml test.ml" *)
49(* End: *)
Note: See TracBrowser for help on using the repository browser.