Tutorial: read pair selectors from the inside out
![For [[5,220],[10,350],[20,400]], the first book pair differs from its page count, and a book ID differs from a maximum page count.](/images/computational-thinking/week-6/library-selectors.png)
| Expression on a non-empty ascending page list | Meaning |
|---|---|
| first(books) | A whole [ID,Pages] pair with minimum pages |
| last(books) | A whole pair with maximum pages |
| last(first(books)) | Minimum page count |
| last(last(books)) | Maximum page count |
| first(last(books)) | ID of a maximum-page book |
The last expression is an identifier, not the least page count. Values can accidentally coincide numerically; answer by the expression’s meaning and schema, not one coincidence. Ties may give several minimum or maximum books, and the insertion tie rule decides which equal pair is at an endpoint.
For the final multiple-select question, the correct claims are: first(books) corresponds to a minimum-page book; last(last(books)) is the maximum page count; and last(first(books)) is the minimum page count. These require a non-empty list sorted ascending by pages.
Try it yourself
What is first(last([[5,220],[10,350],[20,400]]))?
Show explained answer
20, the ID in the final pair. It is not 400 or 220.
Try it yourself
What is last(first(...)) for that list?
Show explained answer
220, the pages component of the first pair.
Try it yourself
Are these expressions defined for an empty library list?
Show explained answer
Not without an explicit empty-input convention. first and last need elements.
