COMP 15
Programming Assignment 2:
Simple Graphing Program

Assigned:
Friday Sep 10, 1999
Due:
Wednesday Sep 22, 1999


Problem Description

In this assignment you are asked to construct a program to graph the values of a simple function over an interval of inputs. The function to be graphed and the interval should be supplied as inputs to your program.

1. The Basic Assignment

Construct the function
   simpleGraph :: (Int -> Int) -> Int -> Int  -> String
If f is a Haskell function of type Int -> Int and a and b are integer values, then simpleGraph f a b should compute a string that, when printed using putStr, shows the value of function f at successive integer inputs between a and b, inclusively. To show the value of f on an input i, a string of stars of length equal to the value of f on input i should be used. For example, given
  f :: Int -> Int
  f i = (i `mod` 3) + 1
the string produced by simpleGraph f 0 5 should be
  "*\n**\n***\n*\n**\n***\n"
which, when printed using putStr (simpleGraph f 0 5), produces
  *
  **
  ***
  *
  **
  ***
Notice that the printed result has six lines corresponding to the six inputs 0, 1, 2, 3, 4, 5.

Your program must signal an error if the input interval is empty, i.e. if a > b. Your program should also signal an error if the value of the function to be graphed falls outside the range [0..40] on any input in the interval.

2. The Optional Embellishment

For extra credit, you can construct the function
   graph :: (Int -> Int) -> Int -> Int  -> IO ()
that improves on simpleGraph by adding axes and labels to the output graph, and by printing the graph directly instead of requiring the use of putStr. To illustrate, given the function
   g :: Int -> Int
   g i = round (20 + 20 * sin (0.3 * fromInt i))
the result of graph g 0 25 should be
     0        10        20        30        40
     |---------|---------|---------|---------|
   0-|********************
     |**************************
     |*******************************
     |************************************
     |***************************************
   5-|****************************************
     |***************************************
     |*************************************
     |**********************************
     |*****************************
  10-|***********************
     |*****************
     |***********
     |******
     |***
  15-|
     |
     |*
     |*****
     |*********
  20-|**************
     |********************
     |**************************
     |********************************
     |************************************
  25-|***************************************
The labels on the vertical axis reflect the input interval; only values divisible by 5 should be printed. Signal an error if the input interval falls outside of [-999..999]. The horizontal axis is fixed and is labeled by values from 0 to 40. All labels should be printed right-justified, as shown.


Submission Procedure

The tentative submission times for this assignment are: Please watch for possible time changes in the class announcements. The submission procedure will be the same as that followed for Program 1.


$ Revised: Thu Sep 9 1999 by prins@cs.unc.edu