(* ** Course: Concepts of Programming Languages (BU CAS CS 320) ** Semester: Summer I, 2009 ** Instructor: Hongwei Xi (hwxi AT cs DOT bu DOT edu) *) // // Assignment Two (Due: Monday, June 1, 2009) // // Total points: 140 // (* ****** ****** *) (* 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: 60 points The following is a description of {\em Game-of-24}. Given four natural numbers $n_1,n_2,n_3$ and $n_4$, one chooses two of them and generates a {\em rational} number $r_1$ using either addition, subtraction, multiplication or division; one mixes $r_1$ with the remaining two numbers and chooses two of them to generate a rational number $r_2$ using either addition, subtraction, multiplication or division; one then takes $r_2$ and the last remaining number to get a rational number $r_3$ using addition, subtraction, multiplication, or division; if there is a way to make $r_3$ equal to $24$, then we say that $(n_1,n_2,n_3,n_4)$ is a good quad. For instance, $(10,10,4,4)$ is a good quad since we have \[(10 * 10 - 4) / 4 = 24\] Similarly, $(5,7,7,11)$ is a good quad since we have \[(5 - 11 / 7) * 7 = 24\] Game-of-24 is a game that determines whether four given natural numbers are a good quad. Please implement a program in ATS that takes four given natural numbers and returns a value indicating whether the four given natural numbers are a good quad. In the case that they are, the returned value should represent an arithmetic expression that attests to their being a good quad. *) datatype exp = | EXPint of int | EXPadd of (exp, exp) | EXPsub of (exp, exp) | EXPmul of (exp, exp) | EXPdiv of (exp, exp) datatype expopt = EXPOPTsome of exp | EXPOPTnone of () fun play24 (n1: int, n2: int, n3: int, n4: int): expopt (* ****** ****** *) (* Exercise 5: 40 points A positive natural number $n$ is congruent if $n$ is the area of a right triangle whose three sides are rational numbers. For instance, $5$ is congruent since it is the area of the right triangle with three sides $3/2,20/3$ and $41/6$; $6$ is congruent since it is the area of the right triangle with three sides $3,4$ and $5$ ($6$ is also the area of the triangle with three sides $7/10,120/7$ and $1201/70$). For your amusement, the right triangle with the following sides $X,Y$ and $Z$, which is computed by D. Zagier, attests to $157$ being congruent (You can readily verify that $X^2+Y^2=Z^2$ and $\frac{1}{2}{X\cdot Y}=157$ in ATS for the following $X,Y$ and $Z$). \[\begin{array}{rcl} X & = & \frac{6803298487826435051217540}{411340519227716149383203} \\[6pt] Y & = & \frac{411340519227716149383203}{21666555693714761309610} \\[6pt] Z & = & \frac{224403517704336969924557513090674863160948472041}{8912332268928859588025535178967163570016480830} \\[6pt] \end{array}\] Please implement a program in ATS to find at least $15$ congruent numbers between $10$ and $50$. Note that you need to provide at least one right triangle for each congruent number such that the three sides of the right triangle are rationals and the area of the right triangle equals the congruent number. *) (* ****** ****** *) (* end of [assignment2.sats] *)