Computer Science 3675
Section 001
Fall 2011
Programming Assignment 2

Assigned: September 14
Due: September 21, 11:59pm


Overview of assignment

For this assignment, you will write the inc, sum and product functions in a functional style instead of an imperative style. You should find the function definitions fairly short and simple. As before, please keep track of the amount of time that you spend writing this. Write a comment at the top of your submission containing an estimate of the amount of time spent.


Language

The language that you will use is Cinnameg (version 7.1). There is material in the book on Cinnameg, and you can find documentation here. It is installed on the Linux virtual machines (login.cs.ecu.edu, xlogin.cs.ecu.edu) as /usr/local/bin/cmgc [the compiler] and /usr/local/bin/cmgr [the interpreter].

Cinnameg is not a purely functional language, and it allows you to use other styles. For this assignment, stay within the functional subset for writing the arithmetic functions. Do not use Relet. Do not use loops, or anything that changes something once you have already created it. Write in a declarative, equational style. You are allowed to use Let to bind an identifier to a value.


Representing nonnegative integers

A nonnegative integer can be represented in binary as a list of zeros and ones. The most convenient way to do so is to put the least significant bit first. For example, the binary number 11012 (13 decimal) would be represented by the list [1,0,1,1].

Of course, list [1,0,1,1,0] also represents 13. Leading zeros in the standard representation (as in 01101) are ignored, and they become trailing zeros in the list representation. Say that a list is normalized if it does not end on 0. So [1,0,1,1] is normalized, but [1,0,1,1,0] is not. Note that list [0] is not normalized, but [ ] is. The empty list is a representation of 0.


The assignment

Your assignment is to write functions inc, sum, and product that work on lists as representations of binary numbers.

  1. inc(x) produces x + 1 (as a normalized list);

  2. sum(x,y) produces x + y (as a normalized list);

  3. product(x,y) produces the product x * y (as a normalized list)

For example inc([1,1,0,1,0,1]) = [0,0,1,1,0,1], and sum([1,1], [1,1]) = [0,1,1]. The result list of each function should be normalized even if the parameter lists are not. For example, inc([1,0,0,0]) = [0,1].

You can handle normalization as follows. An example for inc should suffice. First, write an implementation of inc that does not worry about normalizing its result. You might call it incNN, for inc-non-normalizing. Now define inc as follows.

   Define inc x = normalize(incNN x).
Do the same for the other functions. Function normalize is provided for you. It just removes any trailing zeroes from the list


Requirement

Cinnameg allows large integers. But for this assigment, you are expected to handle the algorithms for adding and multiplying directly using lists of bits. It is not acceptable to convert from a list to an integer, do the arithmetic on the integers, and then convert back to a list.


Writing and running the program

Write one package that defines and exports your definitions. Do not export the helper functions that do not normalize their results. See arithmetic.cmg for a skeleton of this module.

Please include at least one example for each function. I have written inc for you. You will need to provide sum and product.

To test your functions, use a separate module. See here for a skeleton of a testing module. Add a few tests in addition to the examples in the implementation module.

You will find Cinnameg installed for Linux, as /usr/local/bin/cmgc (the compiler) and /usr/local/bin/cmgr (the interpreter).

To compile arithmetic.cmg, use command

  cmgc arithmetic
(To compile without a listing, use
  cmgc -l arithmetic
where the option is dash-lower-case-ell.) The interpreter is called cmgr. You will want to compile both arithmetic.cmg and arithtest.cmg and then run arithtest. To do that use commands
  cmgc -l arithmetic
  cmgc -l arithtest
  cmgr -t arithtest
Option -t of cmgr tells cmgr not to perform some optimizations that would hide information from you if the program encounters an error.


Submitting your work

Be sure to include your name in the program. Also, near the top of the program, write the amount of time spent (in hours). When you are ready to turn in your work, use command

   ~abrahamsonk/3675/bin/submit 2 arithmetic.cmg arithtest.cmg
on a Linux machine such as xlogin.cs.ecu.edu.