// // 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 #2 // Due Thursday, January 31, 2013 at 11:59pm // *) (* ****** ****** *) // // Problem 2: 30 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{a:t@ype} mylist_append (xs: mylist a, ys: mylist a) = Append (xs, ys) fun{a:t@ype} mylist_reverse (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} // 5 points mylist_length (xs: mylist a): int fun{a:t@ype} // 5 points mylist_is_empty (xs: mylist a): bool fun{a:t@ype} // 10 points mylist_nth (xs: mylist a, n: int): a fun{a:t@ype} // 10 points mylist2list0 (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 of [assignment2_mylist.sats] *)