Assignment 1: Easy as Pi


The goal of this assignment is to introduce you to the basics of O'Caml 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 O'Caml and installing a development environment/editor such as emacs or eclipse. Once you have OCaml installed, simply type ocaml from a Unix shell (or type C-c C-b from inside .ml file in emacs). you should now see the Ocaml prompt:

$ ocaml
        Objective Caml version 3.12.0


If you type

"are we there yet?";;
you should get the output:
- : string = "are we there yet?"

Congratulations! If you got this far, Ocaml works. Type

at the prompt (including the # sign) or Ctrl-D to exit Ocaml. Now you just have to learn how to write useful programs.

Part 3: O'Caml Basics

To begin the assignment, simply download the file a1.tgz. Decompress and untar the package using the following command at a unix prompt:

$ tar xfz a1.tgz

Now, cd in to directory a1 and look at the questions contained within the file. Answer them. Make sure the entire file type checks before you hand it in. To type check and compile, you can simply type "make" at a shell prompt:

$ make

Important notes

  1. Compile errors: Your solution must compile. If you omit problems, 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. Magic and its order: You may encounter comments in our template files of the form:
    (*>* Some text here *>*)
    These are magical incantations to our automated grader. Please leave them intact, and make sure to insert your code at the appropriate point in the file (relative to the magic). Do not remove magic, even if you omit problems. If your solutions are out of order, and/or you modify/add/remove magic, we will probably have to test your code manually, which will make us sad.
  3. Testing: How should you write tests? This is largely up to you -- there are a combination of appropriate techniques. Note, you should always think first and code second. Testing will usually involve:
    • Writing a set of test expressions or test functions in each .ml file.
    • Exploring using the O'Caml top-level interpreter.
    • Writing assertions. (See for example, the O'Caml manual on assert statements here).
    To help with testing and debugging, I often craft a set of convenient functions before I begin. For example:
    (* call to print and ensure string is emitted to stdout prior to proceeding *)
    let debug (s:string) : unit =  print_string s; flush_all();;
    (* stop execution; 
     * use when a particular control-flow point should be unreachable *)
    exception Impossible of string;;
    let impossible (s:string) : unit = raise (Impossible s);;
    (* if b is true continue, otherwise raise Assume with s *)
    exception Assume of string;;
    let assume (b:bool) (s:string) : unit = if b then () else raise (Assume s);;
  4. 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.
  5. 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 TAs and professor at office hours.
  6. Line limit: One important element of style is having at most 80 characters per line. We have provided an ocaml script, tarred up here width.tgz, so that you can test whether or not your code satisfies this constraint. Just type make in the directory to compile the code and generate an executable.

Hand In

Hand in your assignment using dropbox here.

Before submitting, it is very important that you compile your code by running the make command while in your a1 directory, and then run your compiled program using ./a1
These steps will check that your code as a whole compiles and that the tests you have written using assert pass. Evaluating definitions in the toplevel, while useful during development, is not sufficient for these purposes. Submissions that do not compile will receive no credit!

Start early. Hopefully you don't need it on the first assignment, but you can refresh your memory about the late submission policy here.