7. Review 3

More sorting, folding, Maybe review. Ch 6 material.

This review covers a little bit of Maybe, sorting, and folding. It is designed to be done on paper not the computer.

Maybe

  1. You might buy something at the store. If your money is at least 100, buy “Jeans”. If your money is at least 30, buy “Shirt”. Otherwise, buy nothing. Write the shop function.

     shop :: Int -> Maybe String
    
  2. You work in a store and your job is to go to Customer Service, pick up any item there, and return it to its place on the shelf. “Soft drink” goes in aisle 10. “Toilet paper” is aisle 7. Any other item goes to aisle 11. If there is nothing to return, then go to aisle 1 and straighten up the area. Write the aisle function.

     aisle :: Maybe String -> Int
    
  3. Yinhong is considering writing a Haskell function that might take in an Int, an ordered pair of Ints, or nothing at all. The output should be the number, the sum of the numbers, or 0 (respectively). Will Yinhong encounter any issues writing this function? Write the signature and function, explaining any issues that arise.

Data I

We will work with a list of Point. The point (width, height) represents a rectangle.

  1. Sort a list of rectangles based on their area (smaller rectangles come first). Break ties by using the perimeter (smaller perimeter comes first). Write the ptHelp helper function used below.

     sortRect :: [Point] -> [Point]
     sortRect rs = sortBy ptHelp rs
    
  2. (Advanced) You can measure how close a rectangle is to a square by comparing the length of the diagonal of rectangle to the diagonal of a square with the same area. Use this method to sort a list of rectangles so that the ones that are more like squares come earlier in the list.

     sortLikeSquares :: [Point] -> [Point]
     sortLikeSquares rs = sortBy sqHelp rs
    

Data II

In this section, we will work with a database of information about apples in a warehouse. Each row of the table is an Apple.

type Apple = (String, Int, String, Double, Int)

The table of apple data looks like this:

Name Origin Year Inventor Cost (per pound) Inventory (lb)
Golden Delicious 1914 Mullins 1.99 50
Honeycrisp 1974 U of MN 2.99 100
Ambrosia 1997 Mennell 2.50 75

You can assume that there are already helper functions name, year, inventor, cost and inv (for inventory).

  1. Write the function val, which takes in an Apple and produces the retail value of the inventory by multiplying. (What makes sense to multiply?) Are there any potential pitfalls in writing this function?

  2. Using foldl, write a total_value function by writing a folding helper function.

     total_value :: [Apple] -> Double
     total_value info = foldl total_value_h 0 info
    
  3. Using sortBy, write a function to sort a list of Apple so that the items with the most inventory appear first in the list, breaking ties by putting the most expensive unit cost first.

Misc

  1. Given a list of points, write a fold helper function that will produce a Picture of radius 3 outline black circles with centers at each of the points.

     circ :: [Point] -> Picture
     circ pts = foldl circhelp coordinateGrid pts
    

Discussion

  1. What are the types of each input to foldl? What is the purpose of each input?

  2. Give a one sentence summary of the “big picture” view of a fold helper function.

  3. What is the signature of the helper function used by sortBy?

  4. What is wrong with this program?

    f :: Maybe Int -> Int
    f y@(Just x)
      | y == Nothing  = 0
      | otherwise     = 10*x
    

More Maybe Practice

  1. (mdup) Write a function that makes a list of n copies of a string, but produces nothing if either the inputs is nothing.

    mdup :: Maybe Int -> Maybe String -> Maybe [String]
    mdup (Just 3) (Just "Que") = ["Que","Que","Que"]
    
  2. (mmap) Write a function that is like map f except that if the function f produces Nothing, then no answer is added to the output list.

    mmap f [0..4] == [1,9]
      where f n | even n = Nothing
                | odd n  = Just (n*n)
    
Last modified November 7, 2024: Review and decent cleaned up test. (2fcd912)