(* ** Course: Concepts of Programming Languages (BU CAS CS 320) ** Semester: Summer I, 2009 ** Instructor: Hongwei Xi (hwxi AT cs DOT bu DOT edu) *) // // Assignment Four (Due: Tuesday, June 16, 2009) // // Total points: 130 points // // Assgn4Ex1: 10 points // Assgn4Ex2: 10 points // Assgn4Ex3: 30 points // Assgn4Ex4: 30 points // Assgn4Ex5: 20 points // Assgn4Ex6: 30 points // (* ****** ****** *) (* // Assgn4Ex1: 10 points // The following is a well-known series: // // ln 2 = 1 - 1/2 + 1/3 - 1/4 + ... // // Please implement a stream consisting of all the partial sums of this // series. Then compute an accurate approximation to ln2 by using Euler's // transform. *) val theAssgn4Ex1Stream : stream double val ln2 : double (* ****** ****** *) (* // Assgn4Ex2: 10 points // For each $i\geq 1$, we use $P_i$ for the $i^{\it th}$ prime number. For // instance, $P_1$ is $2$, $P_2$ is $3$ and $P_3$ is $5$. Please implement a // stream consisting of all the sums of the form // $\Sigma_{i=1}^{n}\frac{1}{P_i}$ for $n\geq 1$. *) val theAssgn4Ex2Stream : stream double (* ****** ****** *) (* // Assgn4Ex3: 30 points // Implement a procedure that takes two matrices and returns their // product. Note that two matrices can be multiplied only if they are of // dimensions $p\times q$ and $q \times r$ for some natural numbers $p,q,r$. *) typedef matrx = list0 (list0 double) fun mul_matrx_matrx (A: matrx, B: matrx): matrx (* ****** ****** *) (* // Assgn4Ex4: 30 points // A natural number $n$ is a Ramanujan number if there exist two distinct // pairs of natural numbers $(i_1,j_1)$ and $(i_2,j_2)$ such that // $n=i_1^3+j_1^3=i_2^3+j_2^3$. For instance, $1729$ is a Ramanujan number as // $1729=1^3+12^3=9^3+10^3$. Please construct a stream of {\em all} Ramanujan // numbers and then use it to find the first twenty Ramanujan numbers. *) val theRamanujanStream: stream int val theFirstTwentyRamanujanNumbers : list0 int (* ****** ****** *) (* // Assgn4Ex5: 20 points // Please write a function that extracts out the (first) longest line in a given file. // The implementation should be stream-based. *) fun longest_line_get (inp: FILEref): string (* ****** ****** *) // // Assgn4Ex6: 30 points // datatype ltree (a:t@ype) = | ltree_nil (a) of () (* // it was incorrectly declared as follows: | ltree_cons (a) of (a, int(*size*), ref (ltree a), ref (ltree a)) *) | ltree_cons (a) of (a, ref int(*size*), ref (ltree a), ref (ltree a)) // end of [ltree] // Please implement in-place left (BST) rotation fun{a:t@ype} ltree_rotate_l (t: ltree a): ltree a // Please implement in-place right (BST) rotation fun{a:t@ype} ltree_rotate_r (t: ltree a): ltree a // Please implement a function that changes the root of a given tree to // a randomly choosen node inside the tree: fun{a:t@ype} ltree_random_root (t: ltree a): ltree a (* ****** ****** *) (* end of [assignment4.sats] *)