2025-11-16 Backpack Review
More review exercises covering the fundamentals. Originally from enrichment.
-
Handcan hold aLeftinteger or aRightstring. Write a data definition forHand. -
Backpackis a data type that holds an Int numPencils, a Double money and a String owner. Write the data definition. -
Update the contents of the backpack. If you get a left hand, add the int to the number of pencils. If you get a right hand, set the owner to be the string.
update :: Backpack -> Hand -> BackpackUse different methods:
- Pattern matching
- Updater/accessor functions.
-
Write a pencil counting function: how many pencils are there total, in all of the backpacks.
pc :: [Backpack] -> Int- List comprehension
- Fold
- Recursion
Simpler Review Problems
-
Use
mapto change a list of words into a list of their lengths.q5 ["cat","be"] == [3,2] -
Use
filterto remove all words longer than 6 letters from a list of words.q6 ["Workaholic","student","study","now"] == ["study","now"] -
Use
foldlto concatenate all of the strings in a list, except skip “Bob”.q7 ["Bob"," ran ","fast"] == " ran fast"