(* ** Course: Concepts of Programming Languages (BU CAS CS 320) ** Semester: Summer I, 2010 ** Instructor: Hongwei Xi (hwxi AT cs DOT bu DOT edu) *) // // Assignment Three (Due: Wednesday, June 9, 2010) // // Total points: 120 + 20 (extra) // (* ****** ****** *) #define ATS_STALOADFLAG 0 // no staloading at run-time (* ****** ****** *) // // assgn3ex1: 10 points // It is assumed that [f] is continous on [x0, x1] and f(x0) * f(x1) <= 0 // fun solve_bisection ( f: double - double , x0: double, x1: double , relprec: double // relative precision ) : double (* ****** ****** *) // // assgn3ex2: 10 points // It is assumed that [f1] is the derivative of [f] // fun solve_NewtonRaphson ( f: double - double , f1: double - double , x0: double , relprec: double // relative precision ) : double (* ****** ****** *) exception Empty of () exception Subscript of () (* ****** ****** *) // // assgn3ex3: 60 points + 20 points (extra) // abstype bignum_t (* implementation is given *) fun bignum_make_int (i: int): bignum_t (* implementation is given *) fun neg_bignum (x: bignum_t): bignum_t // 5 points fun succ_bignum (x: bignum_t): bignum_t // 5 points fun pred_bignum (x: bignum_t): bignum_t (* implementation is given *) fun add_bignum_bignum (x1: bignum_t, x2: bignum_t): bignum_t // 10 points fun sub_bignum_bignum (x1: bignum_t, x2: bignum_t): bignum_t // 10 points fun mul_bignum_bignum (x1: bignum_t, x2: bignum_t): bignum_t // 15 points fun div_bignum_bignum (x1: bignum_t, x2: bignum_t): bignum_t fun divrem_bignum_bignum (x1: bignum_t, x2: bignum_t): @(bignum_t, bignum_t) // 15 points fun sqrt_bignum (x: bignum_t): bignum_t (* implementation is given *) fun eq_bignum_bignum (x1: bignum_t, x2: bignum_t): bool (* // 20 points (extra) Assume X, Y and Z are rational numbers defined as follows: X = 411340519227716149383203 / 21666555693714761309610 Y = 6803298487826435051217540 / 411340519227716149383203 Z = 224403517704336969924557513090674863160948472041 / 8912332268928859588025535178967163570016480830 Please use the above [bignum] implementation to verify that X^2 + Y^2 = Z^2 holds. *) (* ****** ****** *) (* // // assgn3ex4: 40 points // The following is a description of {\em Game-of-24}. Given four natural numbers $n_1,n_2,n_3$ and $n_4$, one chooses two of them and generates a {\em rational} number $r_1$ using either addition, subtraction, multiplication or division; one mixes $r_1$ with the remaining two numbers and chooses two of them to generate a rational number $r_2$ using either addition, subtraction, multiplication or division; one then takes $r_2$ and the last remaining number to get a rational number $r_3$ using addition, subtraction, multiplication, or division; if there is a way to make $r_3$ equal to $24$, then we say that $(n_1,n_2,n_3,n_4)$ is a good quad. For instance, $(10,10,4,4)$ is a good quad since we have \[(10 * 10 - 4) / 4 = 24\] Similarly, $(5,7,7,11)$ is a good quad since we have \[(5 - 11 / 7) * 7 = 24\] Game-of-24 is a game that determines whether four given natural numbers are a good quad. Please implement a program in ATS that takes four given natural numbers and returns a value indicating whether the four given natural numbers are a good quad. If they are, the returned value should represent an arithmetic expression that attests to their being a good quad. *) datatype exp = | EXPnum of double | EXPadd of (exp, exp) | EXPsub of (exp, exp) | EXPmul of (exp, exp) | EXPdiv of (exp, exp) // end of [exp] datatype expopt = EXPOPTsome of exp | EXPOPTnone of () fun play24 (n1: int, n2: int, n3: int, n4: int): expopt (* ****** ****** *) (* end of [assignment3.sats] *)