Assignment 1: Easy as Pi


The goal of this assignment is to introduce you to the basics of OCaml programming. Along the way, you'll discover a couple of algorithms for estimating the value of pi.

Part 1: Online Tasks

Part 2: Setting up your Development Environment

Check out the course resources web site for help getting starting with OCaml and installing or configuring a development environment for your chosen editor, such as emacs, vim, or atom. Once you have OCaml installed and your development tools set up, now you just have to learn how to write useful programs!

Part 3: Assignment Basics

To get started, simply download the tarball a1.tgz. Unzip and untar the code and data by typing the following commands at a unix prompt.

$ tar xfz a1.tgz

A directory called a1 should appear. Now, cd in to directory a1 and look at its contents. i.e., type:

$ cd a1
$ ls -a
You should see the following files:
  • the main file for your assignment. Look inside the file to find a series of short problems you should solve.
  • .merlin: a file that defines the dependencies for program --- useful when using merlin mode in an editor such as emacs or vim. This file is unnecessary if you have not configured merlin for your editor.

You can compile by typing:

$ ocamlbuild a1.d.byte

This will do two things. First, it creates the executable file a1.d.byte. Second, it creates a directory called _build full of auxiliary files used by the OCaml compiler system. To run the executable we have created, type:

$ ./a1.d.byte

If you haven't touched any of the code inside yet, you should see the following message printed out:

Fatal error: exception Failure("undefined")

That indicates the Failure exception has been raised. Follow the instructions in to eliminate the error and complete the assignment.

To delete the _build directory and remove the executable files you have created, type

$ ocamlbuild -clean

This assignments asks you to answer the questions contained within the file. Make sure the entire file type checks and compiles before you hand it in.

Important notes

  1. Compilation errors: Your solution must compile. If you omit problems (you shouldn't!), leave the corresponding definitions commented out so that they will not confuse our automated grader as it checks the other problems. If you are having difficulty getting your code to compile, please visit office hours!
  2. Testing: How should you write tests? At some point during the semester, we may talk about testing in more depth than you saw in the first lecture. For now, we will mostly leave testing up to you (though please follow the instructions in the file and write some tests where we ask). Follow the good practices you have learned in earlier COS courses.
  3. Auxiliary functions: Feel free to use them when they make your code cleaner, more modular, and/or easier to test — that's what functions are for! Just make sure that if we name a particular function that you have to write (either in the assignment text, or in a template file), that you preserve its name so that our automated grader can find it.
  4. Style: Finally, please pay attention to style. Take the extra time to think about the problems and find the most elegant solutions before coding them up. Many of the problems in this assignment are somewhat artificial. Nevertheless, think about your coding style where appropriate (and certainly, thinking about style in the final question computing pi is appropriate). For reference, consult the COS 326 style guide and the lecture notes. In addition, you are also encouraged to bring style questions to the course staff at office hours.
  5. Line limit: One important element of style is having at most 80 characters per line. This is important for our ability to read your code.

Hand In

Hand in your assignment using the dropbox link on the assignment page.

We will give you some indication that you have not drastically misinterpreted the specification by running some tests of our own when you submit. These are not a complete evaluation, and the grading scripts will be more rigorous. Do your own testing; don't rely on our tests to validate your code.

It is very important that you compile and test your code by running the commands shown in Part 3 while in your a1 directory. These steps will check that your code as a whole compiles and that the tests pass. Using merlin (or the toplevel) for typechecking is very useful but is not sufficient for these purposes. Submissions that do not compile will earn very few points!

Start early! Work hard!