CMSC 330, Summer 2012

Organization of Programming Languages

Project 3

Due Monday, June 25, 2012

UPDATE 6/23 & 6/24: Several new public test cases have been added for your convenience.

UPDATE 6/22: For part 2, the itob function should accept zero, and return [] or [false]. Also, the submit server test setup had some issues, which have now been resolved.


This project will give you practice writing code in OCaml.

Do not use imperative style in your programs; this means no explicit loops or memory access.

What to Submit

You should submit three files,,, and

You can find a p3 directory here that contains a .submit file:

Part 1: Simple OCaml functions

Put your solutions to this part in
  • Write a function prod l : int list -> int that returns the product of the elements of l. The function prod should return 1 if the list is empty.

  • Write a function index l e : 'a list -> 'a -> int which takes a list and a single element and returns the position of the last occurrence of that element in the list (indexed by zero). You should return -1 if the element is not in the list.

  • Write a function unzip l : ('a*'b) list -> ('a list)*('b list) that given a list of pairs, returns a pair of lists with the elements in the same order. For example, unzip [(1, 2); (3, 4)] = ([1; 3], [2;4]).

  • Write a function app_int f m n : (int->'a)->int->int->'a list that returns the list [f m; f (m+1); ...; f n]. It should return the empty list if n<m

Part 2: More interesting OCaml functions

Put your solutions to this part in
  • As you know, unsigned integers can be represented in binary notation. We can represent a binary number by a list of booleans, where true means one and false means zero, with the least significant "digit" first. For example, we might represent decimal 12 (binary 1100) as [false; false; true; true] or decimal 6 (binary 110) as [false; true; true]
    • Write a function itob : int -> bool list to convert a positive (or zero) OCaml integer to a list of booleans, following the encoding above. (You may assume the argument to itob is positive or zero.)
    • Write a function btoi : bool list -> int to convert from a list of booleans to an OCaml integer.
    • Write a function shift : bool list -> bool -> bool list that performs a logic shift on the boolean value. This function should take a list and a boolean value indicating if the shift is a left shift or right shift (use true for right shifts and false for left shifts). Note that the meanings of "left" and "right" refer to the binary number written in most significant bit order, while the lists are stored in least significant bit order. Assume that the space used to store the given list is the full space available, so the input and output should be lists of the same length.
    • Write a function add : bool list -> bool list -> bool list to add two lists of booleans. Use only boolean and list operations.

    Note: none of the binary operations should use other binary operations that you've already made.

  • In Squaresville, streets are numbered 1..M and the perpendicular avenues are numbered 1..N. Your task is to write a function

    paths f m n : (int->int->bool)->int->int->int

    that computes the number of ways to get from mth street at nth avenue to 1st street at 1st avenue without going backwards, meaning you always move to a lower numbered street or avenue. During the searching of paths, print out each obtained path from mth street at nth avenue to 1st street at 1st avenue as a string in the format "(m,n)(s,a)(s',a')...(1,1)", where each tuple represents an intersection on the path during the searching. Each string for a path takes one line in the outputs. Note that there are m + n - 1 tuples in each output path.

    Unfortunately, some of the intersections are blocked. The first argument to paths, f, is a function such that f i j returns true if ith street at jth avenue is blocked, and false otherwise.

    Here are some example cases you might want to try.

    • m = 5, n = 7, f = fun i j -> false
    • m = 5, n = 7, f = fun i j -> (i=3) && (j=4)
    • m = 10, n = 10, f = fun i j -> (i mod 2 = 0) && (j mod 2 = 0)

    Note: paths(fun i j -> false) 1 1 should return 1. Though you cannot move through a blocked intersection , you can start at one, so paths(fun i j -> true) 1 1 should also return 1.

    Note: the order for printing the paths does not matter. You only need to find out the right number of paths and print out each path correctly.

Part 3: Frequency Counts

Put your solution to this part in

For this part, write a program that computes frequency counts for all characters in an input file. The input to your program is a text file. As output, your program first prints a line that lists the characters that appeared in the file, in the order in which the characters are first seen. Then after that line is a list of the number of times each character appears, ordered by number of occurrences of each character. In particular:

  1. We will compile your file with ocamlc, and then run the resulting a.out file to execute your program.
  2. Your program should read its input file from standard input and write its output to standard output. The input file will contain 0 or more lines of text. (There are lots of ways that you can do this, some will make your life easier than others... check out the OCaml modules. The ^D (or control D) shown in the input is how that input stream is ended.)
  3. Lower and upper case letters are equivalent and should be printed as upper case characters.
  4. All whitespace characters (tab, space, newline) count as the single "space" character. All other characters count as separate characters. The space should be sorted in the same position as Ocaml ' '.
  5. Your program should first print one line containing each unique character appearing in the file, in the order that each character is first seen. No other characters appear on this line. The "space" character should be printed as the five letters space.
  6. After printing the line containing all the characters, for each character in that line print a line with the following format:

    Character[x] appears y times.
    with x and y replaced by the appropriate values. y is an integer with no leading 0. For the "space" character, x should be space.

  7. The frequency counts should be printed in decreasing order of occurrence.
  8. If two frequency counts are the same, print them in lexicographic (i.e., alphabetical) order. Space comes before any letter in the alphabet. Special characters should be printed in their ASCII order.
  9. Do NOT use built-in OCaml array or hash table libraries in your solution, although if you wish to implement these structures yourself, feel free to do so using lists and tuples.


(the ^D means type control-D)
% ocamlc
% ./a.out
foo: bar
baz qux
Character[space] appears 4 times.
Character[A] appears 2 times.
Character[B] appears 2 times.
Character[O] appears 2 times.
Character[:] appears 1 times.
Character[F] appears 1 times.
Character[Q] appears 1 times.
Character[R] appears 1 times.
Character[U] appears 1 times.
Character[X] appears 1 times.
Character[Z] appears 1 times.

Academic Integrity

The Campus Senate has adopted a policy asking students to include the following statement on each assignment in every course: "I pledge on my honor that I have not given or received any unauthorized assistance on this assignment." Consequently your program is requested to contain this pledge in a comment near the top.

Please carefully read the academic honesty section of the course syllabus. Any evidence of impermissible cooperation on projects, use of disallowed materials or resources, or unauthorized use of computer accounts, will be submitted to the Student Honor Council, which could result in an XF for the course, or suspension or expulsion from the University. Be sure you understand what you are and what you are not permitted to do in regards to academic integrity when it comes to project assignments. These policies apply to all students, and the Student Honor Council does not consider lack of knowledge of the policies to be a defense for violating them. Full information is found in the course syllabus---please review it at this time.

Valid HTML 4.01!