Course Links

Resources

External

This lab is a first look at OCaml. It is designed as a self-contained tutorial: instructions and everything you need to know are contained within comments in the file you will be editing. By the end of this lab, you will have some practice with OCaml datatypes and expressions. You will also have the chance to compile and run a program.

Activity

Download the files from Moodle at this link. This should give you a zip archive containing two files: assert.ml and leapYear.ml. You will not modify assert.ml -- all of your work is contained within leapYear.ml. However, you will need to include both files together when you compile.

Before beginning any work with the program code, you should make sure that your working environment is set up properly. OCaml should already be installed. You should open up a command line interface. From the command line, navigate to the directory where your downloaded files are stored. (I recommend creating a folder for each lab, within a folder for all your work in the course.) Before making any modifications, test that you can compile and run your program. The commands to do that are shown in red below -- the first line compiles the program, and the second runs it.

$ ocamlopt -o leapYear assert.ml leapYear.ml
$ ./leapYear
Running: seconds_in_days 0 ... Test passed!
Running: seconds_in_days 1 ... Test passed!
Running: seconds_in_days 2 ... Test passed!
Running: average seconds per year ...
Test error: `average seconds per year` reported `unimplemented: total_seconds`

Running: total_seconds test1 ...
Test error: `total_seconds test1` reported `dummy test case`

Running: total_seconds test2 ...
Test error: `total_seconds test2` reported `dummy test case`

Running: is_small_prime 47 ... Test passed!
Running: is_small_prime 91 ... Test passed!
Running: is_leap_year 1901 ...
Test error: `is_leap_year 1901` reported `unimplemented: is_leap_year`

Running: is_leap_year test1 ...
Test error: `is_leap_year test1` reported `dummy test case`

Running: is_leap_year test2 ...
Test error: `is_leap_year test2` reported `dummy test case`

Running: days_of_month_no_leap 5 ... Test passed!
Running: days_of_month_no_leap 9 ... Test passed!
Running: days_of_month 2 2012 ...
Test error: `days_of_month 2 2012` reported `unimplemented: is_leap_year`

Running: days_of_month 2 2013 ...
Test error: `days_of_month 2 2013` reported `unimplemented: is_leap_year`

Running: days_of_month 8 2013 ...
Test error: `days_of_month 8 2013` reported `unimplemented: is_leap_year`

Running: add_primes 3 5 7 ...
Test error: `add_primes 3 5 7` reported `unimplemented: add_primes`

Running: add_primes test1 ...
Test error: `add_primes test1` reported `dummy test case`

Running: add_primes test2 ...
Test error: `add_primes test2` reported `dummy test case`

Running: day_of_week 2010 1 1 ...
Test error: `day_of_week 2010 1 1` reported `unimplemented: day_of_week`

Running: day_of_week test1 ...
Test error: `day_of_week test1` reported `dummy test case`

Running: day_of_week test2 ...
Test error: `day_of_week test2` reported `dummy test case`

leapYear.ml: ran to completion

If the compilation doesn't encounter any errors it will finish without any output, as shown here. As you develop your code, it might report errors that you need to fix before proceeding. The program as it runs will report on the code tests performed and whether they passed or failed. Right now it reports a lot of failures because portions of the code aren't finished yet. Your task in this lab is to finish them up (as guided by the comments in the file) so that it passes all the tests when you run the program.

Keep in mind that every time you change your code and save it, you must compile the program without errors before running it again. Otherwise the changes won't be reflected.

ToDo

Open leapYear.ml in a program editor. Read the comments, look at the examples, and follow the instructions therein.

To Submit on Moodle