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 hereThe Main.hs file that implements main1 will import the Util module as follows:
module Main where import Util main = do -- TODO: write implementation hereThe 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 12The output file should be a comma separated value (csv) format with each keyword shown only once with its total sum.
hello,17 goodbye,6Assuming 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
Read and work through the tutorial examples.
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.
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.
cp hw2main1/src/Main.hs hw2main1.hs cp hw2main1/src/Util.hs hw2util1.hs cp hw2main2/src/Main.hs hw2main2.hs cp hw2main2/src/Util.hs hw2util2.hs tar cvf HW2.tar hw2main1.hs hw2util1.hs hw2main2.hs hw2util2.hs
turnin cs453HW2 HW2.tar