[1725989] | 1 | type s = { i : int; } |
---|
| 2 | type fred = I of int | D of float | S of s |
---|
| 3 | type mary = I of int | D of int | S of int |
---|
| 4 | type weekday = Mon | Tue | Wed | Thu | Fri | Sat | Sun |
---|
| 5 | let day : weekday = Mon |
---|
| 6 | let 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 | |
---|
| 17 | let _ = take_class( Mon ); take_class( Sat ); |
---|
| 18 | |
---|
| 19 | type colour = Red | Green of string | Blue of int * float |
---|
| 20 | let c = Red |
---|
| 21 | let _ = match c with Red -> Printf.printf "Red, " |
---|
| 22 | let c = Green( "abc" ) |
---|
| 23 | let _ = match c with Green g -> Printf.printf "%s, " g |
---|
| 24 | let c = Blue( 1, 1.5 ) |
---|
| 25 | let _ = match c with Blue( i, f ) -> Printf.printf "%d %g\n" i f |
---|
| 26 | |
---|
| 27 | let 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 |
---|
| 34 | let _ = check_colour( Red ); check_colour( Green( "xyz" ) ); |
---|
| 35 | |
---|
| 36 | type stringList = Empty | Pair of string * stringList |
---|
| 37 | let 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 | |
---|
| 42 | let _ = for i = 1 to 10 do |
---|
| 43 | Printf.printf "%d, " i |
---|
| 44 | done |
---|
| 45 | |
---|
| 46 | (* Local Variables: *) |
---|
| 47 | (* tab-width: 4 *) |
---|
| 48 | (* compile-command: "ocaml test.ml" *) |
---|
| 49 | (* End: *) |
---|