CS 453 Homework Assignment #2 — Haskell warmup

The Tutorial

Before starting HW2, go work through the Haskell tutorial, which was made specifically for this class.

The Assignment

In this homework assignment you will be writing four Haskell "helper" functions and two separate main programs. The "helper" functions will be used to implement the two main programs.

The prototypes and descriptions for the "helper" functions are as follows:

    -- Returns true if the given String starts with the "while" string 
    -- and false otherwise.
    startsWithWhile :: String -> Bool

    -- Given a string, returns the number of instances of the "while" string.
    countWhile :: String -> Int

    -- Given a list of strings such as ["hello","5","goodbye","6","hello","1"]
    -- creates a list that pairs every other key string with the integer value
    -- for the following number string 
    -- (e.g., [("hello",5),("goodbye",6),("hello",1)].
    findKeyValPairs :: [String] -> [(String,Int)]

    -- Given an existing dictionary stored as a list of key,value pairs,
    -- and a new key,value pair, add the value to the existing value for
    -- the given key or put the given key,value pair into the dictionary.
    -- For example,
    --      updateDict [("hello",5)] ("goodbye",6)
    -- should evaluate to
    --      [("hello",5),("goodbye",6)]
    -- As another example
    --      updateDict [("hello",5),("goodbye",6)] ("goodbye",4)
    -- should evaluate to
    --      [("hello",5),("goodbye",10)]
    updateDict :: [(String,Int)] -> (String,Int) -> [(String,Int)]

The "startsWithWhile" and "countWhile" helper functions should be in the file hw2main1/src/Util.hs. The file should have the following structure:

    module Util where

    startsWithWhile :: String -> Bool
    -- TODO: write the implementation here

    countWhile :: String -> Int
    -- TODO: write the implementation here
The Main.hs file that implements main1 will import the Util module as follows:
    module Main where

    import Util
    
    main = do
        -- TODO: write implementation here
The other two "helper" functions should be in a Util.hs in the hw2main2 project and be used in the main2 program.

The main1 program will read from stdin and print to stdout a count of all instances of the string "while". Assuming you use stack and call the stack project "hw2main1", you should be able to run the program as follows:

    stack exec hw2main1 < infile.txt > outfile.txt

The main2 program will read an input filename from the command line and an output file from the command line. The input file will have keywords and integer counts of those keywords listed on separate lines. For example,

    hello
    5
    goodbye
    6
    hello
    12
The output file should be a comma separated value (csv) format with each keyword shown only once with its total sum.
    hello,17
    goodbye,6
Assuming you use stack and call the stack project "hw2main2", you should be able to run the program as follows:
    stack exec hw2main2 infile.txt outfile.txt

Getting Started

Read and work through the tutorial examples.

Optional Tool: SPOJ

Unlike programming assignments where you are almost entirely responsible for developing a test suite for your compilers, for this homework assignment we have provided a contest in SPOJ that tests all of the examples in the tutorial and all of the "helper" functions and main programs in HW2. Therefore, you can fully test all of your functions on SPOJ before submitting your HW2. See Using SPOJ to test HW2 problems for more details.

Groups for HW2

There should be NO groups for HW2. Everyone should do HW2 by themselves. MOSS is able to compare Haskell programs.

You can create a github repository for HW2 by going to the HW2 assignment in the github classroom for cs453.

Submitting the Assignment

Grading

The grade will be based on how your "helper" functions and the main programs they are used in perform on the same tests that are used in the SPOJ contest.

Late Policy

Late assignments will be accepted up to 24 hours past the due date for a 20% deduction. The assignment will not be accepted past this period. Late means anything after 11:59pm on the day the assignment is due, including 12 midnight and 11:59pm and one second.


mstrout@cs.arizona.edu, 9/1/16