(* ** Course: Concepts of Programming Languages (BU CAS CS 320) ** Semester: Summer I, 2010 ** Instructor: Hongwei Xi (hwxi AT cs DOT bu DOT edu) *) // // Assignment One (Due: Tuesday, May 25, 2010) // (* ****** ****** *) #include "../../code/BUCASCS320.hats" (* ****** ****** *) staload "assignment1.sats" (* ****** ****** *) staload "assignment1_solu.dats" (* ****** ****** *) dynload "assignment1_solu.dats" (* ****** ****** *) implement main () = let // fn print_money (m: money): void = () where { val () = (print "pound= "; print m.pound; print_newline ()) val () = (print "shilling= "; print m.shilling; print_newline ()) val () = (print "pence= "; print m.pence; print_newline ()) } // end of [print_money] val () = (print ("Testing of [assgn1ex1] begins."); print_newline ()) val money1 = '{ pound=2, shilling=3, pence=2 } val money2 = '{ pound=1, shilling=19, pence=11 } val money3 = assgn1ex1_add (money1, money2) (* val () = (print "money3=\n"; print_money money3) *) var err: int = 0 val () = if (money3.pound <> 4) then err := err + 1 val () = if (money3.shilling <> 3) then err := err + 1 val () = if (money3.pence <> 1) then err := err + 1 val () = if (err <> 0) then let val () = prerr "Error: [assgn1ex1_add]: failed" val () = prerr_newline () val () = exit (1) in // empty end // end of [val] val money4 = assgn1ex1_sub (money2, money1) (* val () = (print "money4=\n"; print_money money4) *) var err: int = 0 val () = if (money4.pound <> 0) then err := err + 1 val () = if (money4.shilling <> ~3) then err := err + 1 val () = if (money4.pence <> ~3) then err := err + 1 val () = if (err <> 0) then let val () = prerr "Error: [assgn1ex1_sub]: failed" val () = prerr_newline () val () = exit (1) in // empty end // end of [val] val () = (print ("Testing of [assgn1ex1] is finished successfully."); print_newline ()) // val () = (print ("Testing of [assgn1ex2] begins."); print_newline ()) val gcd = assgn1ex2_gcd (2 * 3 * 3 * 3 * 7 * 7, 3 * 5 * 7 * 11) val () = assert (gcd = 3 * 7) val () = (print ("Testing of [assgn1ex2] is finished successfully."); print_newline ()) // val () = (print ("Testing of [assgn1ex3] begins."); print_newline ()) val () = assert (assgn1ex3_P (10) = 1 << 9) val () = assert (assgn1ex3_P (20) = 1 << 19) val () = (print ("Testing of [assgn1ex3] is finished successfully."); print_newline ()) // val () = (print ("Testing of [assgn1ex4] begins."); print_newline ()) val fib10 = fastfib (10) val () = assert (fib10 = 55) val fib12345678 = fastfib (123456789) val () = assert (fib12345678 = 25624514) val () = (print ("Testing of [assgn1ex4] is finished successfully."); print_newline ()) // #define :: list0_cons #define nil list0_nil // val () = (print ("Testing of [assgn1ex5] begins."); print_newline ()) val () = assert (list_last (xs) = x) where { val x = 5 val xs = (1 :: 2 :: 3 :: 4 :: 5 :: nil): list0 int // val xs = nil // this causes a match failure at run-time } val () = (print ("Testing of [assgn1ex5] is finished successfully."); print_newline ()) // val () = (print ("Testing of [assgn1ex6] begins."); print_newline ()) val xs = 'a' :: 'b' :: nil () val ys = 1 :: 2 :: 3 :: nil () val xys = listprod (xs, ys) val () = loop (xys, 0) where { fun loop (xys: list0 @(char, int), i: int): void = case+ xys of | list0_cons (xy, xys) => loop (xys, i+1) where { val () = if i > 0 then print ", " val () = printf ("(%c, %i)", @(xy.0, xy.1)) } // end of [list0_cons] | list0_nil () => () // end of [loop] } val () = print_newline () val () = (print ("Testing of [assgn1ex6] is finished successfully."); print_newline ()) // in // empty end // end of [main] (* ****** ****** *) (* end of [assignment1_test.dats] *)