For this lab exercise, you will write three small programs as described below. Submit the three source files via Blackboard. This is an individual assignment. You may not work in groups for this lab.
Due Date: 11.59.59pm, Wednesday, Sep. 19th, 2011. Late assignments will be penalized at 10% per day, up to two days. Submitted work after three days will receive no credit.
Implement a program which reads in an integer value,n, and computes the nth Fibonacci number. Once computed, your program should print the value to the screen. Name your program fib.ml.
Recall that the Fibonacci sequence is defined as:
f(0) = 0 f(1) = 1 f(n) = f(n-1) + f(n-2)
Implement a program which reads in two integer values, m and n, then computes the value of the Ackermann function. Once computed, your program should print the value to the screen. Name your program ack.ml.
Recall that the Ackermann function is defined as:
A(m,n) = n + 1 if m = 0 A(m,n) = A(m-1, 1) if m > 0 and n = 0 A(m,n) = A(m-1, A(m, n-1)) if m > 0 and n > 0
Implement the following algorithm in OCaml. Your program should open a file: input.dat, which has a sequence of integers in it. The integers will be one per-line. Read in the integers, sort them and print them out in sorted order (on a single line). Name your program splitter.ml.
The basic steps in the algorithm are:
- Read input into a list.
- Print the input list.
- Call a function, splitter, which takes the input list and splits it into two lists. One list will contain all of the even numbers from the input file and the other contains all of the odd numbers.
- Print out both the even and odd lists.
This program can be implemented without using any assignment statements. Programs without any assignment statements or side-effects are said to bepure functionalprograms. Your solution should be implemented in a functional style, without using unnecessary assignment statements.
Your program should produce the following output:
$ ./splitter input: 7 6 5 4 3 2 1 evens: 2 4 6 odds: 1 3 5 7