2025-11-03 Review Practice

Review exercises on the fundamentals. Originally from advisory. Double length.
  1. Given a list of Int representing distances and a list of String representing the units used to measure, find the total distance in inches. The only units will be “feet” and “inches”.

    nums = [60, 6, 30]
    units = ["inches","feet","inches"]
    check1= [ totalInch nums units == 60 + 6*12 + 30 ]
    

    Solve the same question two ways:

    1. Use recursion with a helper function.
    2. Use zip.
  2. Imagine a width x height grid showing a number y is divisible by a number x. The gridSolid function gives all of the ordered pairs (x,y) with y divisible by x for $1\le x\le \text{width}$ and $1 \le y \le \text{height}$.

  3. The justRemover extracts the information from the Just values in a list and ignores any Nothing.

     justRemover :: [Maybe a] -> a
     check_jr = [ justRemover [Just 5, Nothing, Just 8] == [5, 8] ]
    

New Data Type: Msh

  1. Create a new data type Msh that can hold either a Circle with a Double field called radius or a Rectangle with two Double fields, width and height.

  2. The radiusDbl function doubles the radius of a circle but passes a rectangle through unchanged.

     radiusDbl :: Msh -> Msh
    
  3. The onlyRect function returns a list of the Rectangle items in its input. There is no Circle in its output.

     onlyRect :: [Msh] -> [Msh]
    
  4. The totalArea function finds the total area of all of the items in its input. Use a helper function.

     totalArea :: [Msh] -> Double
    

New Data Type: Drive

  1. Make a new data type Drive that holds a Point called pt, an Int called num, and a String called who.

  2. Write the function illustrated below:

     moremore :: Drive -> Drive
     ckmm =
          moremore (Drive (10.0, 4.0) 12 "Gus") ==
              (Drive (10.0, 4.0) 15 "Gus"
    

    The moremore function updates the numvalue by adding the length of who.

  3. The totalDistfunction finds distances between all of the adjacent points in the list.

    totalDist :: [Drive] -> Double
    totalDist _ = 0
    
    cktd =
      totalDist [ Drive (10.0,20.0) 13 "Ace",
                  Drive (13.0,16.0) 14 "Brown"]
        == 5.0
    

    Bonus: can you do this using zip instead of recursion?

Last modified November 11, 2025: Excellent review. (edaba49)