(* ** Course: Concepts of Programming Languages (BU CAS CS 320) ** Semester: Summer I, 2010 ** Instructor: Hongwei Xi (hwxi AT cs DOT bu DOT edu) *) // // Assignment Two (Due: Wednday, June 2, 2010) // // Total points: 100 + 30(extra) // (* ****** ****** *) (* Exercise 1: 10 points Please implement a function for testing whether a string is a palindrome, that is, whether it equals its mirror image. *) fun palindrome_test (s: string): bool (* ****** ****** *) (* Exercise 2: 15 points Please implement a function for testing whether one string is a permuation of another. An implementation of qudratic time-complexity is acceptable. *) fun string_perm_test (s1: string, s2: string): bool (* ****** ****** *) (* Exercise 3: 15 points The simple rule for translating into "Pig Latin" is to take a word that begins with a vowel and add "yay", while taking any word that begins with one or more consonants and transferring them to the back before appending "ay". For example, "able" becomes "ableyay" and "stripe" becomes "ipestray". Write a function that converts a string of letters into its Pig Latin translation. *) fun piglatin_trans (word: string): string (* ****** ****** *) (* Exercise 4: 20 points Given a natural number n >= 3, please draw a circle and (1) choose n distint points on the circle randomly and (2) connect these points to form a polygon and fill it with whatever color you like. *) (* ****** ****** *) (* Exercise 5: 40 points The following declaration introduces a datatype mylist(a) for representing lists whose elements are of type a: datatype mylist (a:t@ype) = | Nil (a) | Cons (a) of (a, mylist a) | Append (a) of (mylist a, mylist a) | Reverse (a) of mylist a // end of[mylist] The list append and reverse functions can be implemented as follows: fun append {a:t@ype} (xs: mylist a, ys: mylist a) = Append (xs, ys) fun reverse {a:t@ype} (xs: mylist a) = Reverse xs In other words, both [append] and [reverse] are O(1)-time functions, that is, they take constant time to finish. Please implement the following functions: fun{a:t@ype} mylist_length (xs: mylist): int fun{a:t@ype} mylist_is_empty (xs: mylist a): bool fun{a:t@ype} mylist_nth (xs: mylist a, n: int): a fun{a:t@ype} mylist_to_list0 (xs: mylist a): list0 a Note that nth(xs, i) returns the i_th element in the list represented by xs for 0 <= i < n, where n is the length of xs. The meaning of all other functions should be obvious. \end{exercise} *) datatype mylist (a:t@ype) = | Nil (a) of () | Cons (a) of (a, mylist a) | Append (a) of (mylist a, mylist a) | Reverse (a) of mylist a // end of [mylist] fun{a:t@ype} mylist_length (xs: mylist a): int fun{a:t@ype} mylist_is_empty (xs: mylist a): bool fun{a:t@ype} mylist_nth (xs: mylist a, n: int): a fun{a:t@ype} mylist_to_list0 (xs: mylist a): list0 a (* ****** ****** *) (* Exercise 6 (extra): 30 points Please use cairo and gtk to give an illustration of Pascal's theorem. See the file [assgn2ex6.dats] for more details. *) (* ****** ****** *) (* end of [assignment2.sats] *)