Power
Assignment 1: Collatz
Introduction
The goal of this assignment is to introduce you to the basics of OCaml programming and apply them to some simple problems.
Part 1: Online Tasks
- Register for our Ed discussion board. When you have a question about a problem set, Ed 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 or configuring a development environment for your chosen editor, such as emacs, vim, VS Code, 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, download the tarball a1.tgz. Unzip and untar the code and data by typing the following commands at a shell prompt.
$ tar xzf a1.tgz
A directory called a1 should appear. Now, change directory to a1 and look at its contents:
$ 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 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.
- testing.ml: A file with the tests that will be used to evaluate a1.ml.
- README.txt: A readme in which you will record your sources.
You can compile a1.ml
by typing:
$ ocamlbuild a1.nativeThis will do two things. First, it creates the executable file
a1.native
. 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.nativeIf 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
- 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!
- Testing: We have provided tests for most of the
functions you will write in a1.ml. In order to run these
tests, compile with
ocamlbuild testing.native
, and run the resulting executible. - 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 course staff 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 TigerFile link on the assignment page.
We will give you some indication that you have not
drastically misinterpreted the specification by checking
if your solution compiles. These are not tests, make sure
to use the tests in testing.ml
.
It is very important that you compile and test your code 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 interactive toplevel REPL) 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!