Programming Concepts

Chapter 5 Not-Really-A-Quiz

Problem Decomposition

A huge idea in computational thinking is problem decomposition, in which a big problem is broken into smaller, solvable problems. The results of these smaller problems can then be used as inputs to solve the bigger problem.

Problem 5.21 reads as follows:

Write a procedure query that turns a statement into a question by swapping the first two words and adding a question mark to the last word:

> (query '(you are experienced))
(are you experienced?)
  
> (query '(i should have known better))
(should i have known better?)

The solution to the problem might be described, below:

Take the original sentence and make a sentence whose contents are in the following order:

  1. Second word
  2. First word
  3. Everything but the first, second, and last words
  4. The last word with a question mark appended to it (eg., "better" becomes "better?"

First convert this solution description into Scheme functionality, and then write the query function.

  1. Suppose that the parameter you use for query is called sent. What combination of functions will you use to get the verb in the sentence ("are" in the first example, "should" in the second) from sent? (The order in which you use the functions matters!) ________________________________
  2. What function will you use to extract the first word of sent? ________________________________
  3. What function will uyou use to extract the last word of sent? ________________________________
  4. How will you extract everything but the first two words and the last word of sent? ________________________________
  5. What function will allow you to attach a question mark to the last word of sent? ________________________________
  6. What function will you use to put together the pieces of the sentence that is returned by Scheme? ________________________________
  7. Write query.