(* ** Course: Concepts of Programming Languages (BU CAS CS 320) ** Semester: Summer I, 2009 ** Instructor: Hongwei Xi (hwxi AT cs DOT bu DOT edu) *) // // Assignment Three (Due: Monday, June 8, 2009) // // Total points: 180 points // (* ****** ****** *) exception Empty of () exception Subscript of () (* ****** ****** *) (* // assgn3ex1: 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 of (mylist a, mylist a) | Reverse of mylist a 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 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 (* ****** ****** *) // assgn3ex2: 80 points abstype bignum_t (* implementation is given *) fun bignum_make_int (i: int): bignum_t (* implementation is given *) fun neg_bignum (x: bignum_t): bignum_t // 5 points fun succ_bignum (x: bignum_t): bignum_t // 5 points fun pred_bignum (x: bignum_t): bignum_t (* implementation is given *) fun add_bignum_bignum (x1: bignum_t, x2: bignum_t): bignum_t // 10 points fun sub_bignum_bignum (x1: bignum_t, x2: bignum_t): bignum_t // 20 points fun mul_bignum_bignum (x1: bignum_t, x2: bignum_t): bignum_t // 20 points fun div_bignum_bignum (x1: bignum_t, x2: bignum_t): bignum_t fun divrem_bignum_bignum (x1: bignum_t, x2: bignum_t): @(bignum_t, bignum_t) // 20 points fun sqrt_bignum (x: bignum_t): bignum_t (* ****** ****** *) (* // assgn3ex3: 70 points \begin{exercise} (70 points) Given a sequence $s$ consisting of $n$ elements $a_1,\ldots,a_n$, we represent $s$ as follows. \begin{itemize} \item If $n=0$, that is, $s$ is empty, we use $\exp{Empty}$ to represent $s$; \item If $n=2k$ for some $k>0$, we use $\exp{Even (xs)}$ to represent $s$, where $\exp{xs}$ represents the following sequence of pairs; $$(a_1, a_2), \ldots, (a_{n-1}, a_n)$$ \item If $n=2k+1$ for some $k\geq 0$, we use $\exp{Odd (x, xs)}$ to represent $s$, where $x$ represents $a_1$ and $xs$ represents the following sequence of pairs. $$(a_2, a_3), \ldots, (a_{n-1}, a_n)$$ \end{itemize} We use the name {\em random-access list} for such a representation of $l$. The following datatype $\exp{'a~ralist}$ is declared for this kind of representation of list: \begin{verbatim} datatype 'a pair = S of 'a | P of 'a pair * 'a pair datatype 'a ralist = Emp | Evn of 'a ralist | Odd of 'a pair * 'a ralist \end{verbatim} Please implement the following operations on random-access lists. All the implementations should be of $O(\log n)$-time complexity in order to receive full credit. \begin{enumerate} \item (10 points) Given an element $\exp{x}$ and a random-access list $\exp{xs}$, $\exp{racons (x, xs)}$ generates a random-access list whose head and tail are $\exp{x}$ and $\exp{xs}$, respectively. \item (10 points) Given a nonempty random-access list $\exp{xs}$, $\exp{rauncons (xs)}$ return a pair consisting of the head and tail of $\exp{xs}$. \item (20 points) Given a number $\exp{n}$ and a random-access list $\exp{xs}$, $\exp{ralookup (n, xs)}$ returns the $\exp{n}$th element (counting starts with $0$) in $\exp{xs}$ or issues an error if the length of the random-access list is less than or equal to $\exp{n}$. \item (30 points) Given a number $\exp{n}$ and a random-access list $\exp{xs}$, $\exp{raupdate (xs, n, x)}$ updates the nth element (counting starts with $0$) in $\exp{xs}$ with $\exp{x}$ or issues an error if the length of the random-access list is less than or equal to $\exp{n}$. This one is a bit challenging, and the solution may involve the feature of higher-order function. \end{enumerate} The types of these functions are given below: \begin{verbatim} fun{a:t@ype} racons (x: pair a, xs: ralist a): ralist a fun{a:t@ype} rauncons (xs: ralist a): @(pair a, ralist a) fun{a:t@ype] ralookup (xs: ralist a, i: int): pair a fun{a:t@ype} raupdate (xs: ralist a, i: int, x: pair a): ralist a \end{verbatim} \end{exercise} *) datatype pair (a:t@ype) = S(a) of a | P(a) of (pair a, pair a) datatype ralist (a:t@ype) = | Emp(a) | Evn(a) of ralist a | Odd(a) of (pair a, ralist a) // 10 points fun{a:t@ype} racons (x: pair a, xs: ralist a): ralist a // 10 points fun{a:t@ype} rauncons (xs: ralist a): @(pair a, ralist a) // 20 points fun{a:t@ype} ralookup (xs: ralist a, i: int): pair a // 30 points fun{a:t@ype} raupdate (xs: ralist a, i: int, x: pair a): ralist a (* ****** ****** *) (* end of [assignment3.sats] *)