2025-11-03 Review Practice
-
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:
- Use recursion with a helper function.
- Use zip.
-
Imagine a
width x heightgrid showing a numberyis divisible by a numberx. ThegridSolidfunction 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}$. -
The
justRemoverextracts the information from theJustvalues in a list and ignores anyNothing.justRemover :: [Maybe a] -> a check_jr = [ justRemover [Just 5, Nothing, Just 8] == [5, 8] ]
New Data Type: Msh
-
Create a new data type
Mshthat can hold either aCirclewith aDoublefield calledradiusor aRectanglewith twoDoublefields,widthandheight. -
The
radiusDblfunction doubles the radius of a circle but passes a rectangle through unchanged.radiusDbl :: Msh -> Msh -
The
onlyRectfunction returns a list of theRectangleitems in its input. There is noCirclein its output.onlyRect :: [Msh] -> [Msh] -
The
totalAreafunction finds the total area of all of the items in its input. Use a helper function.totalArea :: [Msh] -> Double
New Data Type: Drive
-
Make a new data type
Drivethat holds aPointcalledpt, anIntcallednum, and aStringcalledwho. -
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
moremorefunction updates thenumvalue by adding the length ofwho. -
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.0Bonus: can you do this using
zipinstead of recursion?