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

structs & unions
hw04

Due:
Feb.20 (Sun.)Feb.18 (Fri.) 23:59 on D2L and hardcopy at next class.
Your submitted file should be named “hw04.rkt

Some handy predicates: For your signatures, you can use built-in predicates like string?. Some other handy predicates aren't built-in, but feel free to use them anyway: For some handy type-predicates to help your signatures, natural?, non-negative-real?, and non-empty-string?. And if you want to actually call those functions, you can easily write them yourself, or you can download (right-click) and then (require "student-extras.rkt").

a struct

(This is the completion of hw03a, from D2L.)
Consider the following information, which might be part of a sports-simulation:

A team is:

  1. Follow steps 1-3 of the design recipe (including template). Remember that a datatype definition can be a define-struct which includes, after each field-name, a type-predicate in an expression-comment #;. Give at least three examples of a team.

  2. We’ll say one team is “greater” than another if its offense is higher than the other’s defense, and its defense is higher than the other’s offense. Write the function team>? to implement this. Follow all the steps of the design recipe1. As always, be sure to think about enough test-cases to cover the interesting situations (half of the pts). (In this case, six or seven should suffice, if they each cover a different situation.)

  3. Write merger, which returns the result of merging of two teams. The resulting (new) has the higher offense of the two teams, the higher defense, and the name of the better team: More precisely, use the second team's name if it is team>? the first team, else use the first team's name.

a struct containing a struct

A match-result involves two teams and the resulting score (e.g. 7 to 6). Give a datatype definition for this. Write the function upset? which takes in a match and returns whether the losing team (if any) was team>? than the winner.

a union containing a union

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:

  1. Give appropriate datatype definitions for life-signs, and for rover-measurements. Remember that a datatype definition can be a predicate-function (-> any/c boolean?) which tells if a value is of the indicated-type, as we did in lecture when we defined course-result? and taxable-income-entry?.
    hint: For life-signs, I used symbols for chemicals, and strings for specie-names. This let my code distinguish the two variants easily, as types.
  2. 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.

    Write the function lifesign-strength.

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

  3. 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 5% of earth's level, increasing linearly4 until they are totally newsworthy at 25% (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:


1 Note that the stub-function, step 5e, gets replaced with the real body (step 7), so it won’t be visible in what you submit. Also, you don’t need to include the names of constructors/selectors; that was part of quiz03 but not part of the design recipe.      
2 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.      
3 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. Oopsie.      
4 "linearly" meaning: if an oxygen-level is (say) a three-quarters of the way from the-level-to-have-0-newsworthiness and the-level-to-have-1-newsworthiness, then that level has 0.75 newsworthiness.      

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.