A file browser uses two data definitions:
Each folder's list contains exactly its immediate children. A list of three children therefore has three Cons values followed by one Empty(). The structure shown has no shared items or cycles.
The following Python function templates use pattern matching: each case selects a data form and gives names to its fields.
def scan_item(item): match item: case File(size): return 1 case Folder(contents): return scan_list(contents) def scan_list(items): match items: case Empty(): return 0 case Cons(first, rest): return scan_item(first) + scan_list(rest)
Starting with scan_item(root), determine the total number of calls to scan_item, the total number of calls to scan_list, and the number of scan_list calls whose input is Empty(). Include the initial call and every terminating call. Give all three counts as exact integers.
Unlock full course notes by signing up
Decide which fields require a case check
A meal-order program uses dictionaries with a kind entry. Its data definition permits Counter(item_count) and Delivered(item_count, distance_km). Every valid value contains a positive integer item count; only Delivered values contain a nonnegative delivery distance. Assuming inputs satisfy this definition, which statements about function structure are true? Select all that apply.
Complete a recursive counting template
A temperature logger represents its readings using the following data definition:
Readings is either Empty or Link(value, next).
value is an integer temperature in degrees Celsius.
Count the values a definition permits
An access-control program uses the following finite data definitions:
Grant has two fields: level, either viewer or editor; and expiry, either one day, one week, or one month.
Decision is either Deny, which has no fields, or Allow(grant), which contains one Grant.
Repair a nested-alternative calculation
A shipping program accepts valid parcel dictionaries. A Local parcel contains kind="local" and a positive weight_kg. An International parcel contains kind="international", a positive weight_kg, and a declaration dictionary. That declaration is either Documents, with only kind="documents", or Goods, with kind="goods" and a nonnegative value_dollars.
Local shipping costs 5.00 dollars per kilogram. International shipping costs 12.00 dollars plus 7.00 dollars per kilogram, with an additional 4% of declared value for Goods only. The following function fails for some valid inputs. Which correction makes it implement the stated charges for every permitted form?