PA-H01: Haskell Programmming Assignment 1

First, you’re going to need to set up Haskell on your machine or use Haskell from the ALAMODE Linux Lab (BB 136) where it is already set up. Instructions for setting up Haskell on your own machine can be found here.

Note

These notes are applicable to all Haskell assignments. Please read carefully before you start the assignment, they are important.

  • You must name your functions exactly as written on this page. This includes spelling and case of the function name. The functions are automatically graded, and the autograder script will not be able to find your function if you do not name it correctly.
  • You may be required to use a certain Haskell feature to solve a problem. If the question asks for this, you must use that feature in a non-trivial manner to receive full points for the problem. For example, I make ask you to use a list comprehension. If the assignment asks for it to be implemented that way, you must implement it that way.
  • Unless otherwise asked, your functions must handle the case where you are passed an empty list (or other structure) correctly. For example, if asked to find the number of odd elements in an empty list, the correct answer should be 0 (not an error).
  • In functions that return a string or produce output, you must match the output formatting correctly, byte-for-byte (unless otherwise noted).
  • You should test more cases than listed on this page. The autograder has a large number of test cases, not all of which you see here. I am unable to give students all of the test cases that the autograder uses as then you could (in theory) just hard-code the answers. If you are having issues on a specific test case, come in during office hours and we can look at it.
  • You only get 4 submissions on the autograder before you will have to Email me and ask for more. Use them wisely, and test your code before uploading.

Part I: List Comprehensions

Create the following functions based on list comprehensions (2 points each):

  1. negsInList: returns the number of negative elements in a list:

    *Main> negsInList [1,2,3]
    0
    *Main> negsInList [1,-2,3]
    1
    
  2. oddsInList: returns the number of odd elements in a list:

    *Main> oddsInList [2,4,6]
    0
    *Main> oddsInList [1,2,3]
    2
    
  3. capsInList: returns the number of capital letters in a list:

    *Main> capsInList ['a','A','b','B']
    2
    *Main> capsInList ['a','b']
    0
    
  4. sumOdd: returns the sum of the odd numbers in a list:

    *Main> sumOdd [1,2,3,4,5]
    9
    

Part II: Pairs

Assume you have a list of pairs, where the first element represents a quantity and the second element represents a price, such as:

receipt = [(3, 4), (5, 6)]

Create the following functions to process such a list (2 points/function):

  1. getQuantities: returns a list of the quantities (i.e., first elements) in the list.
  2. getPrices: returns a list of the prices (i.e., second elements) in the list
  3. getTotal: sums \(\mathrm{quantity} \times \mathrm{price}\) for the list (e.g., with the above list, this would return 42)
*Main> receipt
[(3,4),(5,6)]
*Main> getQuantities receipt
[3,5]
*Main> getPrices receipt
[4,6]
*Main> getTotal receipt
42

Part III: Lists of Lists

Given a list of lists, such as:

list = [[1,2,3],[4,5]]

Create the following functions (2 points each):

  1. doubleAll: doubles all elements. Try a nested list comprehension.
  2. doubleFirstList: doubles all elements in only first list
  3. doubleSecondList: doubles all elements in only the second list
  4. sumListItems: returns a list whose elements are the sums of the values in the sublists. Try a nested list comprehension, or, play around with map for some fun.
*Main> list
[[1,2,3],[4,5]]
*Main> doubleAll list
[[2,4,6],[8,10]]
*Main> doubleFirstList list
[2,4,6]
*Main> doubleSecondList list
[8,10]
*Main> sumListItems list
[6,9]

Note you are not required to use any list comprehensions for this part, just my suggestion for an easy way to solve this problem.

Submit

Before submitting, read the important notes at the top of this assignment.

Do not write a module ... where line for this assignment (kudos if you know what it is though!)

Submit your .hs file on Gradescope.