// // Title: Concepts of Programming Languages // Number: CAS CS 320 // Semester: Fall 2012 // Class Time: TR 12:30-2:00 // Instructor: Hongwei Xi (hwxiATcsDOTbuDOTedu) // Teaching Fellow: Alex Ren (arenATcsDOTbuDOTedu) // (* // // Assignment #8 // Due Tuesday, April 9, 2013 at 11:59pm // Total points: 50 // *) (* ****** ****** *) #include "BUCASCS320-8.hats" (* ****** ****** *) staload "eulertrans.sats" staload "assignment8.sats" (* ****** ****** *) local fun aux ( sum: double, sgn: int, denom: double ) : stream double = \$delay ( let val sum1 = sum + sgn / denom in stream_cons (sum1, aux (sum1, ~sgn, denom+1)) end ) // end of [aux] in (* in of [local] *) implement genAssgn5Ex1Stream () = aux (0.0, 1, 1.0) end // end of [local] val ln2seq = genAssgn5Ex1Stream () val ln2seq2 = let // fun aux (xs: stream double): stream double = \$delay ( let val-stream_cons (x, xs2) = !xs in stream_cons (x, aux (euler_trans (xs2))) end ) in aux (ln2seq) end // end of [val] implement ln2 () = stream_nth (ln2seq2, 5) (* ****** ****** *) (* implement ln2 () = ... *) (* ****** ****** *) typedef int2 = @(int, int) fn cubesum (ii: int2):<> int = let val x0 = ii.0 and x1 = ii.1 in x0*x0*x0 + x1*x1*x1 end // end of [cubesum] (* ****** ****** *) typedef streamint2 = stream (int2) (* ****** ****** *) local fun genStream ( x: int, y: int ) : streamint2 = let in // \$delay ( stream_cons ((x, y), genStream (x+1, y+1)) ) // end // end of [genStream] fun genSStream (y: int): stream (streamint2) = let in // \$delay (stream_cons (genStream (1, y), genSStream (y+1))) // end // end of [genSStream] fun lte_cubesum ( x1: int2, x2: int2 ) : bool = cubesum (x1) <= cubesum (x2) // end of [lte_cubesum] fun ordmergeSStream ( xss: stream (streamint2) ) : streamint2 = \$delay ( let // val-stream_cons (xs, xss) = !xss val-stream_cons (x(*fst*), xs) = !xs // HX: crucial!!! // in // stream_cons ( x, stream_ordmerge_fun (xs, ordmergeSStream (xss), lte_cubesum) ) // end ) // end of [ordmergeSStream] fun ramanfilter (xs: streamint2): stream_con (int) = let val-stream_cons (x0, xs) = !xs in ramanfilter2 (cubesum(x0), xs) end // end of [ramanfilter] and ramanfilter2 ( s0: int, xs: streamint2 ) : stream_con (int) = let val-stream_cons (x1, xs) = !xs; val s1 = cubesum (x1) in // if s0 < s1 then ramanfilter2 (s1, xs) else stream_cons (s0, \$delay (ramanfilter2 (s1, xs))) // end of [if] // end // end of [ramanfilter2] in (* in of [local] *) implement genAllRamanujanNumbers () = \$delay (ramanfilter (ordmergeSStream (genSStream (1)))) implement fprintRamanujanNumbers (out, n) = let // fun loop ( out: FILEref, n: int, xs: stream (int) ) : void = let in // if n > 0 then let val-stream_cons (x, xs) = !xs val () = fprint_int (out, x) val () = fprint_newline (out) in loop (out, n-1, xs) end else () // end of [if] // end // end of [loop] // in loop (out, n, genAllRamanujanNumbers ()) end // end of [fprintRamanujanNumbers] end // end of [local] (* ****** ****** *) implement main () = let // val out = stdout_ref val () = fprintln! (out, "ln2 () = ", ln2()) val () = fprintRamanujanNumbers (out, 20) // in end // end of [main] (* ****** ****** *) (* end of [assignment8.dats] *)