CS 1101 Computer Science I
Spring 2016

Computer Science Department
The Morrissey College of Arts and Sciences
Boston College

About Staff Textbook Grading Schedule Resources
Notes Labs Piazza Canvas GitHub Problem Sets
Manual StdLib Pervasives UniLib OCaml.org
Problem Set 2: Repetition

Assigned: Tuesday January 26, 2016
Due: Thursday February 4, 2016, midnight
Points: 16 Points up to 22 Points Total
Points: 13 Points up to 15 Points Total

This is a pair problem set. You must work with a partner. If you don't know anyone yet (perfectly understandable!), you can use the Piazza partner finding tool or you can ask a staffer to help you find a partner.

This problem set consists of 3 parts 2 parts. In Part 1, you'll write a few simple functions working on booleans, strings and numbers. In Part 2, there are 5 required 2-point problems A through E and one optional part F.

Setup

Start by downloading the harness code. Put the compressed archive in a handy place and unpack it. Next, rename the folder from 02.ps.harness to your surname followed by the digit 2. (Mine would be named Muller2.)

The problems in the part1 and part2 folders both have Makefiles and can be compiled using the Unix make command as in:

	  > cd Muller1/part1/
	  > make
	  ...
	  > ./part1
      

All of the problems in both parts are couched in terms of writing a specific function. Each function has a stub including failwith "Not yet implemented". In order to solve the problem, replace the stub with your code.

Part 1 (3 Points Total): Working with Booleans, Strings and Numbers

  1. (1 Point) Write a function oneButNotTheOther : bool -> bool -> bool such that the call (oneButNotTheOther b1 b2) returns true if exactly one of b1 or b2 is true, otherwise it should return false. For example, the call (oneButNotTheOther true true) should return false, the call (oneButNotTheOther false true) should return true.

  2. (1 Point) Using the library function Random.int : int -> int, write a function randBetween : int -> int -> int such that a call (randBetween lo hi) returns a randomly selected integer that is between lo and hi. Note that the integer value returned can be any integer from lo up to one less than hi.

  3. (1 Point) Write a function longer : string -> string -> string such that the call (longer s1 s2) return a string indicating which of s1 or s2 was longer. If s1 is longer, the longer function should return the string "first". If s2 is longer, the longer function should return the string "second". If the two strings are of the same length, the longer function should return the string "they are equal".

Part 2 (10 Points up to 12 Total): Working with Drawings

The problems in Part 2, labeled A through F, all involve writing functions that use some of the Universe functions to draw images in the graphics window. As we've discussed in class, the graphics window is Cartesian or xy-plane, but with y=0 at the top rather than the bottom.

Parts C, D and E involve drawing rings in the graphics window; all are worth 2 points. Part F requires a drawing of squares running on a diagonal. Parts A, B, C and D are relatively simple and can be done right away. Parts E and F require repetition. Note that Part F is optional extra credit.

The problems in Part 2 refer to the following images.

Figure A Figure B Figure C
Figure D Figure E Figure F

Part A (2 Points) twoColors

Write a function twoColors : unit -> Color.t such that a call (twoColors()) has equal chances of returning either Color.black or a randomly generated color.

Part B (2 Points) randomStripe

Write a function randomStripe : unit -> Image.t such that a call (randomStripe()) returns a square the size of the graphics window with a horizontal stripe that is half the height of the graphics window. The stripe should be centered and should have a 50% chance of being Color.black and 50% chance of being a random color.

Part C (2 Points) ring

Write a function ring : float -> float -> Color.t -> Image.t such that a call (ring radius width color) returns a ring with a radius, width and color as given.

Part D (2 Points) Random Rings

Write a function randomRing : Image.t -> Image.t such that a call (randomRing image) returns a new image that is just like the input image but it contains a newly placed random ring. By "random ring", we mean a ring with a random radius, random width, random color and which is placed at random (x, y) coordinates in image.

Part E (2 Points) rowOfRings

Write a function rowOfRings : int -> Image.t such that a call (rowOfRings n) returns a rectangle containing n rings with equal diameters and with random widths and random color. The height of the rectangle should be the diameter of the rings and the width should be the displayWidth.

Part F (2 Points) Diamonds (optional extra credit)

Write a function diamonds : int -> Image.t such that a call (diamonds n) returns an image containing n randomly colored squares of equal size running from upper left to lower right.

PART 3 MOVED, Now Part 2 of Problem Set 3

Created on 01-19-2016 23:09.