CS 430: Lecture 10 - Haskell Midterm Review

Dear students:

Today we have a practice exam for the upcoming Haskell midterm. A number of you have submitted candidate questions, and I have collected most of them below. I left out several that would take too much time, were similar to others, or lacked solutions.

Problems

  1. Write a function join that concatenates a list of strings together.
  2. Write a function startEnd that takes in a list of any type and returns a 2-tuple containing the first and last element of the list.
  3. Write function box that takes a list of Int triples, each representing the length, width, and height of a box in inches. The function returns the total volume of all items as a string in the form "42276in^3".
  4. Write function percentWeight that takes a list of weights and returns a list of percentages of the total weight of all items as strings. For example percentWeight [12, 24, 36, 48] yields ["10.0%","20.0%","30.0%","40.0%"].
  5. Write a function middle3 that accepts a list and returns a list of just the three middle elements.
  6. Assume you have a file named words.txt containing one word per line. Define a main function that reads in the file and prints out each word and its length with a sentence of the form WORD has COUNT characters. For example, suppose the file contains this text:
    Computer
    Science
    Homework
    Your program should produce this output:
    Computer has 8 characters
    Science has 7 characters
    Homework has 8 characters
  7. Fill in the blanks in the code below. Use Maybe to communicate an invalid result when the side lengths are negative.
    main = do
      let result = pythagorean (1, 2)
      case result of
        ______ -> print c
        ______ -> putStrLn "Error: Both values have to be greater than or equal to zero."
     
    pythagorean :: ______ -> ______
    pythagorean (a, b) =
      if a >= 0 && b >= 0 then
        ______ $ sqrt $ a ** 2 + b ** 2
      else
        ______
  8. Write a record type named Business with the one and only constructor Business. Give it a name field of type String, a description field of type String, and an employees field of type Int. Have your type derive the Show typeclass. Create also a toInfo function that takes in a Business and returns a string of the form: "Business: Ron's Trucking with 500 employees".
  9. Suppose you are given an array of employee salaries at AmaGoogle, Inc. Word just came down from management that these salaries must be tripled right away. Write a function named tripleSalaries that accepts a list of salaries and returns the list of salaries with their values tripled.
  10. Write a main function that accepts three command-line arguments: a number, an arithmetic operator, and another number. It prints the value of the expression.
  11. Write a main function that accepts a list of file paths as command-line arguments. It prints the first line of each file.
  12. Write a function reach that accepts a list of numbers as a parameter and returns the distance between its least and greatest values.
  13. Your boss just sent you a list that contains thousands of integers. Your task is to sort them by value, and then write that sorted list to a file named sorted.txt. You only have until 5 PM or you risk being fired, so you aim for a solution that requires as little code as possible.
  14. Define a Genre enum to describe a type of television channel, with variants for news, sports, and cartoons. Define a Channel data type with a single variant named Channel that has fields for the channel's genre and number.
  15. Define type Run to represent a repeating character. Give it two variants. Single has a field for a character that appears just once. Multiple has a field for a count and a character that appears a specified number of times. For example, Single '@' represents the string "@", and Multiple 4 '@' represents "@@@@". Write function decodeRun to produce a run's string. Write function decodeRuns to decode a list of runs and join their result into a single string.
  16. Write a main function that accepts three command-line arguments: a path to an input file, an integer \(n\), and a path to an output file. It writes to the output file only the lines of the input file that have at least \(n\) characters.
  17. Write a main function reads in a file of space-separated integers and multiplies each by a number passed as a command-line argument.
  18. Write an impure function printBiggersOfTwo that accepts a list of pairs and prints the larger of each on its own line. In the case of a tie, print the first component.
  19. Write a function squareDigits that accepts a String and returns a list of the squares of any digits contained in the String. For example, squareDigits "asd3_ui9d3" yields [9, 81, 9]. Use higher- order functions. Use isDigit from Data.Char to identify digits.
  20. Write a function swapPairs that accepts a list of tuples and returns a new list of pairs with the first and second elements of each pair swapped.
  21. Write a main function reads in a file of space-separated integers and prints their sum.
  22. Define a Robot data type that has variants Walking and Talking. Both variants have fields name and battery of types String and Int, respectively. Walking robots also have a field distance of type Int. Talkings robots also have a field volume of type Int. Make Robot an instance of the Show typeclass.
  23. Suppose you have two lists, one of (String, String, String) and one of Double. They are of equal size. You want to replace the third string in each tuple with its corresponding Double. Write a function replaceThird that accomplishes this task.

TODO

Here's your list of things to do in the near future:

Wrap up work on project 2. For your final progress report, prepare a 2-3 minute screencast of your project that briefly demonstrates its execution. Give voiceover commentary. Pick three programming language ideas and reflect on how they are exhibited in your source code. Upload the video to a streaming service of your choice and share a link to it in your group channel on Discord. Also commit and push to GitHub. Do all this before noon Monday.

See you next time.

Sincerely,