(* ** Course: Concepts of Programming Languages (BU CAS CS 320) ** Semester: Summer I, 2009 ** Instructor: Hongwei Xi (hwxi AT cs DOT bu DOT edu) *) (* ****** ****** *) staload "assignment2.sats" (* ****** ****** *) #include "../../code/BUCASCS320.hats" (* ****** ****** *) #define :: list0_cons #define cons list0_cons #define nil list0_nil typedef charlst = list0 char (* ****** ****** *) // assgn2ex1 implement palindrome_test (s) = let val cs = string_explode (s) val cs' = list0_reverse (cs) // [cs] and [cs'] are of the same length fun eq (cs: charlst, cs': charlst): bool = case+ (cs, cs') of | (c :: cs, c' :: cs') => c = c' andalso eq (cs, cs') | (_, _) => true // end of [eq] in eq (cs, cs') end (* end of [palindrome_test] *) (* ****** ****** *) // assgn2ex2 (* ** the time complexity of this function is O(n1 * (n1 + n2)) ** where [n1] and [n2] are lengths of [s1] and [s2], respectively. *) implement string_perm_test (s1, s2) = let // count the number of occurrences of [c] in [cs] fun cnt (cs: charlst, c: char, res: int): int = case+ cs of | c1 :: cs1 => let val res = (if c = c1 then res + 1 else res): int in cnt (cs1, c, res) end // end of [::] | nil () => res // end of [cnt] // testing whether each character in [cs] has the same number // of occurrences in [cs1] as in [cs2] fun test (cs: charlst, cs1: charlst, cs2: charlst): bool = case+ cs of | c :: cs => let val n1 = cnt (cs1, c, 0) and n2 = cnt (cs2, c, 0) in if n1 = n2 then test (cs, cs1, cs2) else false end (* end of [::] *) | nil () => true // end of [test] val cs1 = string_explode (s1) and cs2 = string_explode (s2) in test (cs1, cs1, cs2) end (* end of [string_perm_test] *) (* ****** ****** *) // assgn2ex3 implement piglatin_trans (s) = let fun char_is_vowel (c: char): bool = string_contains ("AaEeIiOoUu", c) val cs = string_explode (s) fun loop (cs: charlst, accu: charlst): charlst = case+ cs of | c :: cs1 => if char_is_vowel (c) then list0_append (cs, list0_reverse accu) else loop (cs1, c :: accu) // end of [if] | nil () => list0_reverse (accu) // end of [loop] in case+ cs of | c :: _ when char_is_vowel c => s + "yay" | _ => let val s = string_implode (loop (cs, nil ())) in s + "ay" end // end of ]_] end (* end of [piglatin_trans] *) (* ****** ****** *) // #include "assgn2ex4_solu.dats" // #include "assgn2ex5_solu.dats" (* ****** ****** *) (* end of [assignment2_solu.dats] *)