Commits

Michael O'Neill committed 3e12808

Initial commit.

Comments (0)

Files changed (3)

+#CSCI 350
+##Lab 1
+
+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.
+
+1. Install the OCaml compiler and runtime on your system. You can find binary
+	distributions at [Inria](http://caml.inria.fr/download.en.html). You can also
+	install this via [Cygwin](http://www.cygwin.com), if you are on Windows.
+
+2. 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)
+
+3. 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
+
+4. 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
+print_endline("Prints the Ackermann function of M and N");;
+print_string("Enter M: ");;
+let m = read_int();;
+print_string("Enter N: ");;
+let n = read_int();;
+
+let rec ack = function
+   | m,n when m < 0 || n < 0 -> invalid_arg "Ackermann's function is only defined over the non-negative integers"
+   | 0,n -> n+1
+   | m,0 -> ack (m-1,1)
+   | m,n -> ack (m-1,ack (m,n-1))
+;;
+
+print_endline(string_of_int (ack (m,n)));;
+print_endline("Prints the Nth Fibonacci Number");;
+print_string("Enter N: ");;
+
+let limit = read_int();;
+
+let rec fib = function
+   | n when n < 0 -> invalid_arg "N must be a positive integer"
+   | 0 -> 0
+   | 1 -> 1
+   | n -> fib(n - 1) + fib(n - 2)
+;;
+
+print_endline(string_of_int (fib limit));;