(* ** Course: Concepts of Programming Languages (BU CAS CS 320) ** Semester: Summer I, 2009 ** Instructor: Hongwei Xi (hwxi AT cs DOT bu DOT edu) *) // // Assignment One (Due: Tuesday, May 26, 2009) // // Total points: 100 // (* ****** ****** *) (* // // Exercise 1 (10 points) // Exercise 2.7: Old English money had 12 pence in a shilling and 20 shillings in a pound. Write functions to add and substract two amounts, working with triples. *) typedef money = '{ pound= int, shilling= int, pence= int } fun assgn1ex1_add (m1: money, m2: money): money // // please note that the following function needs to handle the case // correctly where the amount of [m1] is less than the amount of [m2]: // fun assgn1ex1_sub (m1: money, m2: money): money (* ****** ****** *) (* // // Exercise 2 (20 points) // Exercise 2.19: Declare a function in ATS for computing the Greatest Common Divisor based on the following equations, where m and n range ove positive integers: GCD(m, n) = GCD (n, m) GCD(2m, 2n) = 2 * GCD(m, n) GCD(2m, 2n + 1) = GCD (m, 2n + 1) GCD(2m + 1, 2n + 1) = GCD (n-m, 2m+1) GCD(m, m) = m *) fun assgn1ex2_gcd (m: int, n: int): int (* ****** ****** *) (* // // Exercise 3 (15 points) // Exercise 2.23 P(1) = 1 P(n) = 1 + P(1) + ... + P(n-1) for n > 1 Please implement a function in ATS that computes P(n) for given integers n that are greater than or equal to 1. *) fun assgn1ex3_P (n: int): int (* ****** ****** *) (* // // Exercise 4 (25 points) // For each natural number $n$, let $vFib(n)$ be the vector $(Fib(n), Fib(n+1))^T$, that is, the transpose of the vector $(Fib(n), Fib(n+1))$. Clearly, we have the following equation for each natural number $n$. \[vFib(n+1)=A\cdot vFib(n) \mbox{, where } A=\left(\begin{array}{cc} 0 & 1 \\ 1 & 1 \\ \end{array}\right)\] Hence, we have the equation $vFib(n)=A^n\cdot vFib(0)$ for each natural number $n$. Please use this equation to implement a $\Theta(\log n)$-time and $\Theta(1)$-space procedure in ATS that computes the Fibonacci numbers $Fib(n)$. \def\buid{{\it buid}} \item Please find the last 8 digits of $Fib(\buid)$, where $\buid$ is the 8-digit number in your BU id. Please see the file [assgn1ex4.dats]. *) fun fastfib (n: int): int (* ****** ****** *) (* // // Exercise 5 (10 points) // Exercise 3.2: Write a function to return the last element of a nonempty list. You may assume that the input of this function is a non-empty list. *) fun{a:t@ype} list_last (xs: list0 a): a (* ****** ****** *) (* // // Exercise 6 (20 points) // Please implement a function that returns the product of two given lists. For instance, given lists [a, b] and [1, 2, 3], the product should be the following 6-element list: [(a,1), (a,2), (a, 3), (b, 1), (b, 2), (b, 3)] *) fun{a,b:t@ype} listprod (xs: list0 a, ys: list0 b): list0 @(a, b) (* ****** ****** *) (* end of [assignment1.sats] *)