More conditionals and helper functions

Putting it all together

How can we use the pen cost functions we wrote last time to calculate the total cost of pens?

fun total-cost(num-pens :: Number, slogan :: String) -> Number:
doc: "compute the total cost of pens, including shipping"
  add-shipping(pen-cost(num-pens, slogan))
end

We’re using add-shipping as a helper function: a function that helps us break down a large problem into smaller subproblems. In this case, we have identified two subproblems: computing the base cost of pens, and adding in shipping costs.

Malaria example

You’re working for a public health organization that has been studying malaria prevention in a rural area. Your team has collected data on which people have been hit with malaria cases, and discovered that people who have mosquito nets and either live at least 1000 feet from the lake or have had malaria at least 3 times are at low risk for getting sick.

You have the location of everyone’s house on a coordinate grid maintained by the county. The lake is a circle of radius 40 feet, and its center is at grid location (600 ft, 300 ft).

You want to write a function that takes FOUR inputs about a person: the x and y coordinates of their house, whether they have mosquito nets, and how many times they have had malaria before. The function will return a boolean indicating whether they are low risk.

We spent our class time working on a solution to this problem–you can watch the lecture capture for more detail. We came up with a solution that uses two helper functions: a function to compute the distance from the lake and a function to do the malaria risk computation. The signatures of these functions look like this:

fun distance-from-lake(x :: Number, y :: Number) -> Number:
  ...
end

fun low-risk-distance(feet-to-lake :: Number, have-net :: Boolean,
    prev-cases :: Number) -> Boolean:
    ...
end


fun low-risk(x :: Number, y :: Number, have-net :: Boolean,
    prev-cases :: Number) -> Boolean:
  feet-to-lake = distance-from-lake(x, y)
  low-risk-distance(feet-to-lake, have-net, prev-cases)
end

We then worked on the low-risk-distance function. We came up with some test cases:

fun low-risk-distance(feet-to-lake :: Number, have-net :: Boolean,
    prev-cases :: Number) -> Boolean:
    ...
where:
  low-risk-distance(300, false, 50) is false
  low-risk-distance(40000, false, 600) is false
  low-risk-distance(999, true, 2) is false
  low-risk-distance(1001, true, 50) is true
  low-risk-distance(999, true, 50) is true
end

We did an exercise comparing a few possible bodies for the function, shown below:

fun low-risk-distance1(feet-to-lake :: Number, have-net :: Boolean, prev-cases :: Number) 
  -> Boolean:
  if have-net: 
    if prev-cases >= 3:
      true
    else if feet-to-lake > 1000:
      true
    else:
      false
    end
  else:
    false
  end
end

fun low-risk-distance2(feet-to-lake :: Number, have-net :: Boolean, prev-cases :: Number) 
  -> Boolean:
  if have-net: 
    if (prev-cases >= 3) or (feet-to-lake > 1000):
      true
    else:
      false
    end
  else:
    false
  end
end


fun low-risk-distance3(feet-to-lake :: Number, have-net :: Boolean, prev-cases :: Number) 
  -> Boolean:
  have-net and 
  ((prev-cases >= 3) or (feet-to-lake >= 1000))
end