RU beehive logo ITEC dept promo banner
ITEC 380
2020spring
ibarland

working with a union type
hw03

Due: Feb.11 (Tue) in class (hardcopy and D2L)
Your submitted file should be named “hw03.rkt”.

We wish to write code about whether or not the Radford Highlanders won their basketball game, this last weekend. We want to capture: whether they won/lost, or tied, or there wasn't any game this weekend, or (if the game is still in-progress) what the name of the team currently being played is. (Oddly, we will not track the opposing team's name when the game isn't in progress1. Perhaps we don't care about such details once a game is over?)

  1. (5pts) Complete steps 1-3 of the design recipe for representing this information, which we'll name “game-result”.
    Hint: To represent whether they won or lost, a boolean seems appropriate. What types seem appropriate for each of the other variants?
  2. (5pts) Write a function payout which takes in the amount of a bet2 placed that the Highlanders’ would win their game (non-negative) and a game-result and returns how much money is to be repaid:
  3. (5pts) Write a function game-result->string which returns a user-friendly message similar to one of the following, as appropriate: (You will, of course, follow the applicable steps 4,5,7,8 of the design recipe.)
  4. (10 pts) Various rovers on Mars (Curiosity) and the moon (Yutu 2) take scientific measurements, including oxygen-levels in the atmosphere (indicating the capability to support life), and actual signs of life (by-products like methane, or perhaps finding actual bacteria or other life!) Occasionally, the equipment might malfunction and give no result.

    A rover-measurement is one of:

    In turn, a lifesign is one of:

    Some lifesigns are stronger indicators than others; we'll say the "strength" of a lifesign (on a scale of 0 to 1) is: 1.0 for a species, and for by-products it's 0.1 per character in the chemical-name (with a maximum possible value of 1, of course). (Longer, fancier chemical-names are more likely the result of an organic process?) E.g. 'urea has a strength of 0.4; 'ammonia a strength of 0.7; 'monosodium-glutamate (20 characters) would max out at 1.0.

    hint: In addition to string-length and minmax, symbol->string will be helpful.

    Write the function newsworthy, which takes a rover-measurement, and returns how newsworthy it is (on a scale of 0 to 1): A sensor-failure rates 0.10. For a life-sign, the newsworthiness is twice its strength (maxing out at 1, of course). Oxygen levels only start to become newsworthy after 1% of earth's level, increasinging linearly5 until they are totally newsworthy at 21% (or more) of earth's level.

    hint: Figuring out exactly what your code needs to do becomes easier after writing your unit-tests.
    remember: Follow all steps of the design recipe as appropriate, of course. If handling a union-type, and one variant happens to itself be another union-type, a helper-function is more appropriate than having one function which does all the details of two different union types.
    “One function, one task”: If a concept like "lifesign strength" is its own definition (described in its own paragraph above), then good design suggests it’s probably worth its own function, rather than being anonymously folded into a larger function that does even more.


Standard instructions for all homeworks (unless instructed otherwise):


1 Certainly it is natural to want to always know the opposing-team-name even for the other situations (won/lost, tied, etc.). But since we haven't yet introduced compound types (“structs”/“records”/“objects”), we don't know how to do this. So: Don't. Only keep track of the opposing-team-name in the event of game-in-progress.

(And on further thought, we'll also note that while opposing-team-name could conceivably be information worth tracking for several cases, it wouldn't make sense for no-game-this-week. This complicates a correct data-representation; more on that in class.)

     
2 A perfectly perfecty legal bet on a game of skill, with no luck involved whatsoever.      
3 Derivation: The atmosphere is about 21% Oxygen, by volume (i.e. 1lair ≈ 0.21lO2). Also, 1mole of O2 ≈ 22.4l, and 1mole of O2 ≈ 2*16g. So (for O2), 22.4l ≈ 32g ⇒ 1 ≈ 32/22.4 g/l , meaning 1lair * 0.21(lO2/lair) * 32/22.4 g/l ≈ 0.3g.      
4 Of course in reality, if these earth-based lifeforms were actually detected, it'd be most likely they were stowaways from Earth, and they might become an invasive species on another planet. Oops.      
5 "linearly" meaning: if an oxygen-level is (say) a quarter of the way from the-level-to-have-0-newsworthiness and the-level-to-have-1-newsworthiness, then that level has 0.25 newsworthiness.      
6 The only exception to open-paren-preceded-by-space (or, at the start of a line) is any “[(”s occuring in a cond. Though we'll have a few other exceptions in the future: let* expressions, and (very occasionally) when calling higher-order functions.      

logo for creative commons by-attribution license
This page licensed CC-BY 4.0 Ian Barland
Page last generated
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.