Ignore:
Timestamp:
Jun 25, 2024, 9:26:16 PM (3 months ago)
Author:
Peter A. Buhr <pabuhr@…>
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.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

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";
     1open Printf
     2
     3type s = { i : int; j : int }
     4let sv : s = { i = 3; j = 5 }
     5type adt =
     6        I of int |
     7        F of float |
     8        S of s
     9let 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
     15let 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
     21let adtv : adt = I(3) let _ = adtprt( adtv ); silly( adtv )
     22let adtv : adt = F(3.5) let _ = adtprt( adtv ); silly( adtv )
     23let adtv : adt = S(sv) let _ = adtprt( adtv ); silly( adtv )
     24
     25type week = Mon | Tue | Wed | Thu | Fri | Sat | Sun [@@deriving enumerate]
     26let _ = List.iter ( fun e -> printf "%d" (to_val e) ) all_of_week
     27
     28let day : week = Mon
     29
     30let take_class( d : week ) =
     31        if d <= Fri then
     32                printf "weekday\n"
     33        else if d >= Sat then
     34                printf "weekend\n";
    1135        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"
    1640
    1741let _ = take_class( Mon ); take_class( Sat );
    1842
    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
     43type weekday = Mon | Tue | Wed | Thu | Fri
     44type weekend = Sat | Sun of float
     45type week = Weekday of weekday | Weekend of weekend
     46let day : week = Weekend (Sun 3.5)
    2647
    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
     48let 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"
     58let _ = take_class( day )
    4159
    4260let _ = for i = 1 to 10 do
    43         Printf.printf "%d, " i
     61        printf "%d, " i
    4462done
    4563
Note: See TracChangeset for help on using the changeset viewer.