Changeset 343c8be for doc/theses/jiada_liang_MMath/test.ml
- Timestamp:
- Jun 25, 2024, 9:26:16 PM (4 months ago)
- Branches:
- master
- Children:
- d5efcb7
- Parents:
- 089b39e1 (diff), d96d4f0 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/jiada_liang_MMath/test.ml
r089b39e1 r343c8be 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"; 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"; 11 35 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"36 Mon | Wed -> printf "CS442\n" | 37 Tue | Thu -> printf "CS343\n" | 38 Fri -> printf "Tutorial\n" | 39 _ -> printf "Take a break\n" 16 40 17 41 let _ = take_class( Mon ); take_class( Sat ); 18 42 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 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) 26 47 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 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 ) 41 59 42 60 let _ = for i = 1 to 10 do 43 Printf.printf "%d, " i61 printf "%d, " i 44 62 done 45 63
Note: See TracChangeset
for help on using the changeset viewer.