15-212-X : Homework Assignment 1

**Due Wed Sep 11, 10:00 am (electronically); papers at recitation.
**

Maximum Points: 50 (+10 extra credit)

- While we acknowledge that beauty is in the eye of the beholder, you should nonetheless strive for elegance in your code. Not every program which runs deserves full credit. Make sure to state invariants in comments which are sometimes implicit in the informal presentation of an exercise. If auxiliary functions are required, describe concisely what they implement. Do not reinvent wheels and try to make your functions small and easy to understand. Use tasteful layout and avoid longwinded and contorted code. None of the problems requires more than a few lines of SML code.
- Make sure that your file compiles and runs. A program which doesn't run will not get full credit and is likely to incur a heavy penalty.
- Homeworks must be all your own work.
- Late homeworks will be accepted
**only**until start lecture on Thursday, with a 25% penalty. - If you have any questions about the assignment, contact Carsten
Schuermann at
`carsten@cs.cmu.edu`or use`cmu.andrew.academic.15-212-X.discuss`.

First, the factorial function on natural numbers is defined mathematically as follows:

0! | = | 1 | |

n! | = | n * (n-1)! | for n > 0 |

The factorial function is the basis for the first definition of binomial numbers. BN(n,r) denotes the binomial number for (n,r) or "n choose r". It is defined as follows:

BN(n,r) | = | n! --------- r! (n-r)! |
for 0 <= r <= n |

A way to calculate binomial numbers which does not require factorials is to use Pascal's triangle. The triangle has a 1 at its top, every other number in the Pascal triangle is defined as the sum of the numbers to the right and to the left above. If there isn't any number, it counts as zero.

`pascal`

and must have type ` int * int -> int `

.
Calculate the following values ` PT(5,3) `

and ` PT(7,1) `

.
`pascal`

is a correct implementation of the binomial as
defined using factorials. Carefully state and check the boundary
conditions! You don't need to type your proof in (writing it by hand and
handing it in at recitation is usually faster and easier).
Binomial numbers have other somehow surprising properties:

` BN(n,0) + .... + BN(n,n) `

for an arbitrary
n. This sum can be written in closed form. Find the closed form, and prove
it correct. To find the closed form, write an ML program `sumAll`

which is of type `int -> int`

. Experiment with the program and
guess the closed form. Then prove by induction that your claim is correct.
The next problem is very similar to the previous one.

```
BN(n,0) - BN(n,1) + .... + (-1)^n * BN(n,n)
```

again for an arbitrary n. This sum can be written in closed
form. Find the closed form, and prove it correct. To find the closed form,
write an ML program `sumNeg`

which is of type ```
int ->
int
```

. Experiment with the program and guess the closed form. Then
prove by induction that your claim is correct.
```
binomial
```

which uses factorials. This implementation is likely to lead to
rather frequent overflow exceptions. Test it to find some values of n and r
where the computation requires intermediate results which exceed the size
allowed by the machine representation of integers. Compare this to the
function ` pascal`

. Consider how you might define a function to
compute binomials based on multiplication which works for a larger set of
natural numbers. [Of course, eventually the result will be too large to be
representable with machine integers and no further improvement is possible
without another implementation of integers.]
We expect to obtain an approximation value which is close to the integral. It should be clear from the graph above, that the approximation value might be slightly above or below the real value. It is one of the easier theorems of Calculus that with finer partitions we can hope for a more refined approximation result. You can see this, in the next picture where we make one refinement step by splitting every partition on the x-axis in two equal parts. The sum of the red rectangles determines now the approximation value. Note that the light blue areas have been counted for in the previous step, but not any more.

```
integrate : real -> (real -> real) -> (real * real) -> (int
* real)
```

` integrate epsilon f (a,b) `

approximates the integral of f
between a and b up to epsilon. It returns a a pair, where the first component
yields the current refinement level (n, not 2- the sine function between 0 and pi
- the exponential function between 0 and 1
- the function f(x) = cos(2*pi*sin(x)) between 0 and pi

*
Hint: Use the context browser to access information about the
mathematical functions from the
Math structure.
*

`integrate0 : (real -> real) -> (real -> real)`

` integrate0 f `

is the function g(x) which represents
the value of the integral of f between 0 and x, approximated with
bound epsilon.
- Put your SML code into a file named ass1.sml in your ass1 directory. All
of your definitions should be in this one file. Your handin directory is
`/afs/andrew/scs/cs/15-212-X/studentdir/<your andrew id>/ass<number>`

- Hand in your proofs for Question 1.2, 1.3 and 1.4 on paper at the recitation.