Power
Assignment 1: Easy as Pi
Introduction
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
- Register for our Piazza discussion board. When you have a question about a problem set, Piazza will be your go-to source.
- Read the introductory notes on Functional Basics, Type Checking
Part 2: Setting up your Development Environment
Check out the course resources web site for help
getting starting with OCaml
and installing a development environment/editor such as emacs, vim 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).
We'll write the dollar sign ($) at the beginning of a line to indicate the unix prompt
--- you never have to type that. After typing ocaml
, you should now see the
following (modulo your exact version -- anything 4.01 or above is okay):
$ ocaml OCaml version 4.02.1 #
The # is the OCaml prompt. If you type the following at the prompt
"are we there yet?";;you should get the output:
- : string = "are we there yet?"
Congratulations! If you got this far, OCaml works. Type
#quit;;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: 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. ie, type:
$ cd a1 $ ls -aYou should see the following files:
- a1.ml: the main file for your assignment. Look inside the file to find a series of simple problems you should solve.
- .merlin: a file that defines the dependencies for program --- useful when using emacs or vim merlin mode. This file is unnecessary if using a different text editor or Eclipse.
You can compile a1.ml
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 a1.ml 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 a1.ml
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 a1.ml file. Make sure the entire file type checks and compiles before you hand it in.
Important notes
- 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!
- 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.
- 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.
- 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.
- 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.
Before submitting, 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 be severely penalized!
Start early! Work hard!