Exercises: A lesson about program design (20 minutes)

In the first lecture, we talked about a program that searches for political bias in journalism. Part of that program involved seeing whether the labels "liberal" or "conservative" appeared near names of politicians known to be in each category.

  1. Write a function label-near? that takes a political label, a name and three words (all as strings) and produces a boolean indicating whether both the name and the label appear (in any order) within the three words.

  2. Write a function string-one-of? that takes a string of a word to look for and three strings of other words and produces a boolean indicating whether the first word is one of the three other given words. Then write label-near2? that has the same contract and purpose as label-near?, but is implemented using string-one-of?.

    Which of label-near? or label-near2? is preferable and why? What if we decided to expand the search to four adjacent words instead of three -- which program would you rather modify and why? What does this teach you about program design?