Changeset f3b67b6
- Timestamp:
- Jun 25, 2024, 12:09:24 PM (4 months ago)
- Branches:
- master
- Children:
- 6803ff1
- Parents:
- 41f4e2d
- 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"; 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 -
doc/theses/jiada_liang_MMath/test2.cfa
r41f4e2d rf3b67b6 1 1 #include <fstream.hfa> 2 2 #include <stdlib.hfa> 3 #include <enum.hfa> 3 4 4 5 struct MR { double mass, radius; }; 5 6 6 enum( MR ) Planet { 7 enum( MR ) Planet { // typed enumeration 7 8 // mass (kg) radius (km) 8 9 MERCURY = { 0.330_E24, 2.4397_E6 }, 9 10 VENUS = { 4.869_E24, 6.0518_E6 }, 10 11 EARTH = { 5.976_E24, 6.3781_E6 }, 11 MOON = { 7.346_E22, 1.7380_E6 }, 12 MOON = { 7.346_E22, 1.7380_E6 }, // not a planet 12 13 MARS = { 0.642_E24, 3.3972_E6 }, 13 14 JUPITER = { 1898._E24, 71.492_E6 }, … … 15 16 URANUS = { 86.86_E24, 25.559_E6 }, 16 17 NEPTUNE = { 102.4_E24, 24.746_E6 }, 18 PLUTO = { 1.303_E22, 1.1880_E6 }, // not a planet 17 19 }; 18 20 19 enum( double ) { G = 6.6743_E-11 }; 21 enum( double ) { G = 6.6743_E-11 }; // universal gravitational constant (m3 kg-1 s-2) 20 22 21 23 static double surfaceGravity( Planet p ) with( p ) { 22 return G * mass / ( radius \ 2 ); //exponentiation24 return G * mass / ( radius \ 2 ); // no qualification, exponentiation 23 25 } 24 26 static double surfaceWeight( Planet p, double otherMass ) { … … 27 29 28 30 int 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 30 32 31 33 double earthWeight = convert( argv[1] ); 32 34 double earthMass = earthWeight / surfaceGravity( EARTH ); 33 35 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 37 38 case MERCURY, VENUS, EARTH, MARS: 38 sout | label E(p ) | "is a rocky planet";39 sout | label( rp ) | "is a rocky planet"; 39 40 case JUPITER, SATURN, URANUS, NEPTUNE: 40 sout | label E(p ) | "is a gas-giant planet";41 sout | label( rp ) | "is a gas-giant planet"; 41 42 default: 42 sout | label E(p ) | "is not a planet";43 sout | label( rp ) | "is not a planet"; 43 44 } 44 45 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"; 50 49 } 51 50 }
Note: See TracChangeset
for help on using the changeset viewer.