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.
A function using distance_km can read that field before checking the order's kind.
Every function receiving Delivered must read both fields, regardless of its requested result.
Checking that kind is Delivered permits the function to read the distance_km field.
A function returning only item_count can read that field without checking kind.
Unlock full course notes by signing up
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?
Trace calls across nested data definitions
A file browser uses two data definitions:
Item is either File(size) or Folder(contents). File sizes are non-negative integers in bytes, and folder contents are an ItemList.
ItemList is either Empty() or Cons(first, rest), where first is an Item and rest is another ItemList.