Changeset f3b67b6


Ignore:
Timestamp:
Jun 25, 2024, 12:09:24 PM (4 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
6803ff1
Parents:
41f4e2d
Message:

update test programs for different program languages

Location:
doc/theses/jiada_liang_MMath
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • doc/theses/jiada_liang_MMath/test.ml

    r41f4e2d rf3b67b6  
    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
  • doc/theses/jiada_liang_MMath/test2.cfa

    r41f4e2d rf3b67b6  
    11#include <fstream.hfa>
    22#include <stdlib.hfa>
     3#include <enum.hfa>
    34
    45struct MR { double mass, radius; };
    56
    6 enum( MR ) Planet {
     7enum( MR ) Planet {                                                                             // typed enumeration
    78        //          mass (kg)  radius (km)
    89        MERCURY = { 0.330_E24, 2.4397_E6 },
    910        VENUS   = { 4.869_E24, 6.0518_E6 },
    1011        EARTH   = { 5.976_E24, 6.3781_E6 },
    11         MOON    = { 7.346_E22, 1.7380_E6 }, // not a planet
     12        MOON    = { 7.346_E22, 1.7380_E6 },                                     // not a planet
    1213        MARS    = { 0.642_E24, 3.3972_E6 },
    1314        JUPITER = { 1898._E24, 71.492_E6 },
     
    1516        URANUS  = { 86.86_E24, 25.559_E6 },
    1617        NEPTUNE = { 102.4_E24, 24.746_E6 },
     18        PLUTO   = { 1.303_E22, 1.1880_E6 },                                     // not a planet
    1719};
    1820
    19 enum( double ) { G = 6.6743_E-11 }; // universal gravitational constant (m3 kg-1 s-2)
     21enum( double ) { G = 6.6743_E-11 };                                             // universal gravitational constant (m3 kg-1 s-2)
    2022
    2123static double surfaceGravity( Planet p ) with( p ) {
    22         return G * mass / ( radius \ 2 ); // exponentiation
     24        return G * mass / ( radius \ 2 );                                       // no qualification, exponentiation
    2325}
    2426static double surfaceWeight( Planet p, double otherMass ) {
     
    2729
    2830int main( int argc, char * argv[] ) {
    29         if ( argc != 2 ) exit | "Usage: " | argv[0] | "earth-weight";
     31        if ( argc != 2 ) exit | "Usage: " | argv[0] | "earth-weight"; // terminate program
    3032
    3133        double earthWeight = convert( argv[1] );
    3234        double earthMass = earthWeight / surfaceGravity( EARTH );
    3335
    34         Planet p = fromInt( prng( SizeE(Planet) ) ); // select a random orbiting body
    35 //      Planet p = fromInt( prng( 9 ) ); // select a random orbiting body
    36         choose( p ) {
     36        Planet rp = fromInt( prng( countof( Planet ) ) );       // select random orbiting body
     37        choose( rp ) {                                                                          // implicit breaks
    3738          case MERCURY, VENUS, EARTH, MARS:
    38                 sout | labelE( p ) | "is a rocky planet";
     39                sout | label( rp ) | "is a rocky planet";
    3940          case JUPITER, SATURN, URANUS, NEPTUNE:
    40                 sout | labelE( p ) | "is a gas-giant planet";
     41                sout | label( rp ) | "is a gas-giant planet";
    4142          default:
    42                 sout | labelE( p ) | "is not a planet";
     43                sout | label( rp ) | "is not a planet";
    4344        }
    4445
    45 //      for ( Planet p = MERCURY; posE(p) <= posE(NEPTUNE); p = succ( p ) ) {
    46         for ( p; enum Planet ) {
    47                 sout | "Your weight on" | (p == MOON ? "the" : "") | labelE(p)
    48 //              sout | "Your weight on" | labelE(p)
    49                          | "is" | wd( 1,1, surfaceWeight( p, earthMass ) ) | "kg";
     46        for ( p; Planet ) {                                                                     // enumerate
     47                sout | "Your weight on" | ( p == MOON ? "the" : " " ) | label( p )
     48                         | "is" | wd( 1,1,  surfaceWeight( p, earthMass ) ) | "kg";
    5049        }
    5150}
Note: See TracChangeset for help on using the changeset viewer.