Lab 3: ML Programming

Handed out: Feb 8th

Due on: Feb 14th


In this lab we continue to develop ML programming skills by writing more complex functions, including recursive functions. For this lab, you should write all of your functions in one file (such as lab3.sml), and submit it per the TA's instruction.


A triangle can be represented by three numbers, each being the length of a side. With these three numbers, you can determine many properties about a triangle. For this assignment, use integer numbers.

1. Write a function right(a:int, b:int, c:int) that returns the boolean value true if the three values form a right triangle, and false otherwise. Do not assume that any of the a, b, or c is always the hypotenuse. To be a right triangle, the sum of the squares of the two smaller sides must be equal to the square of the largest side, called the hypotenuse (this is the Pythagorean Theorem).

2. Write a function kind(a:int, b:int, c:int) that returns one of three strings: "scalene" if none of the sides are equal, "isosceles" if exactly two sides are equal, and "equilateral" if all three sides are equal.

3. Write a function triangarea(a:int, b:int, c:int) that computes the area of a triangle using the following formula:
                                                      sqrt(s*(s-a)*(s-b)*(s-c))
    where s is the one half of the perimeter of the triangle. The perimeter of a triangle is defined as (a+b+c).


4. Quadratic Roots. Write a function quad_root that finds the roots of a quadratic equation. Any quadratic equation can be represented by a three-tuple (a,b,c), where a is the coefficient of x^2, b is the coefficient of x (x^1), and c is the constant (coefficient of x^0). Your function takes the three parameters a, b, and c, and returns a two-tuple of real numbers, which are the roots of the function.  (Try this after Thursday lecture).

The formula for calculating quadratic roots is

Do not worry about imaginary roots, which occur if 4ac is greater than b^2. That is, assume that there are no imaginary roots.

An example function which returns a two-tuple is given below:

fun f(x:real) = ( x*x , x*x*x );
The above function returns a two-tuple consisting of the square and cube of the parameter x.


Consider the recursive function:

fun C(n,m) = 
   if m=0 orelse m=n then 
      1 
   else 
      C(n-1,m) + C(n-1,m-1);
which computes the number of combinations for selecting m objects from a set of n distinct objects, where 0 <= m <= n.

In the following exercises, we want to practice writing recursive functions.


We define a function factorial such that factorial(n) = 1*2*3*...*n
 

5. Define the factorial function recursively. Assume that factorial(0) = 1 for the base case. For negative numbers, your function should return a 0 as the answer.
 

6. Write down what values you get for factorial of 11, 12, and 13.


Another way to compute the number of combinations for selecting m objects from a set of n distinct objects is by the formula

comb(n,m) = factorial(n) div (factorial(m) * factorial(n-m))
7. Write an ML function comb(n:int, m:int) for computing the number of combinations using the formula given above.

8. Test to see if the two functions C(n,m) and comb(n,m) for computing the number of combinations give the same answers. Test on values of (3,1), (6,3), (7,4), (8,2), (9,5), (10,8), (12,7), (13,10). Do the two functions C(n,m) and comb(n,m) always give the same answers?



9. Write a function called sum that will compute the sum of all numbers in a list of integers. (Think recursively). Thus:
 
                      sum([3,4,5])   should return the answer 12.



10.  Write the following functions for accessing elements of a list  (these functions are not recursive):

                       findfirst
                       findsecond
                       findthird
                       findfourth

 which behave in the following each way. Each of these four functions takes a list as a parameter, and returns the first element, the second element, the third element , and the fourth element respectively.

Thus,    findfirst([5,6,7,8])  returns 5; findsecond([5,6,7.8]) returns 6, findthird([5,6,7,8]) returns 7, while findfourth([5,6,7,8]) returns 8.