7. Review 1

Review questions for Ch 6 and Ch 7. Lambda. Fold. Sorting.

Chapter 6

Common difficult topics: lambda and fold.

Generally familiar topics, but don’t forget: map and filter.

  1. The function r1 is supposed to remove multiples of a given number m from a list nums, and then output a list whose items are m times the squares of the remaining items.

     r1 :: Int -> [Int] -> [Int]
     r1 m nums = []
    

    Write a single (long) line of code that defines r1.

    Example:

     r1 3 [10,12,13,15] == [3*10*10,3*13*13]
    
  2. Review the signature (and purpose) of a general folding function!

    How would you write a folding function to take in a list of words and produce one long word with underscores after each one?

     r2 ["Bram","Stoker","Dracula"] == "Bram_Stoker_Dracula_"
    
  3. Write a folding function r3h that will do all of the work of the r1 function (first question) and add up the items in the output list.

     r3 :: Int -> [Int] -> Int
     r3 m nums = []
    

Chapter 6 Hint

There are two possible directions to take to make r3:

  1. Allow the helper to take in the an additional parameter, m.
  2. Make the helper inside a let or where.

Chapter 7

  1. Which sorting function is the most “fundamental”?

  2. What is the difference between using comparing and using < (less than)?

  3. Unusual string sorting. In this sorting method, the front of a list is the highest priority and length is more important than letters. Write a function to use in sortBy that results in longer words appearing first in the list and break ties in length by ordinary alphabetical order.

  1. High priority messages are important, but long messages take time to send. Given a list of (Priority,String), put it in order based by taking the square of the length of the message and (integer) dividing by the priority.

    1. Write a function to compute this number.

    2. What would be an appropriate sorting method?

      q7 :: [(Int,String)] -> [(Int,String)]
      q7 pns = [] -- you write
      
Last modified October 29, 2024: New review material and typo correction. (a9d002b)