Cluster 5 * Summer 2004
Computer Science Through Games and Scheme Programming
David G. Kay, Dan Frost, Kevin Papke


Scheme Lab Activities: Second Set

  1. Today you will be doing pair programming: This means two people using one computer. One is the "driver," who controls the keyboard and mouse. The other is the "navigator," who observes, asks questions, suggests solutions, and thinks about slightly longer-term strategies. The two people switch roles regularly--every 15 minutes would be a good guideline.
    You may choose your own partners. The TAs will help pair you up if you ask them to. For the afternoon session, you may switch partners (but if both morning partners want to continue the partnership into the afternoon, that's okay, too).
    Now, choose a partner and together, choose one computer to work on.

  2. If you haven't had a chance to finish the first set of Scheme lab activities from yesterday afternoon, finish them now. If you have the slightest question about anything in those assignments, ask the TAs. We can keep everybody coming along at a good pace if we get all the questions answered now.

  3. To check your progress, answer these questions. You and your partner should quiz each other, alternating questions.
    Answer these questions in your head or with pencil and paper. Only then should you check your answer in DrScheme. Remember that the answer itself isn't what's important--it's knowing how to get the answer and why the answer is right. Anybody can just type the expressions into the computer; that's no way to learn anything.

    1. Each of these Scheme expressions has a value (i.e., its result after Scheme processes it). What is the value of each of these expressions? As these get more complicated, just take them meticulously step by step; that's an important programmer's skill.

      1. (* 3 4 5)

      2. (- 10 2 3)

      3. (> 25 12)

      4. (<= 35 34)

      5. (> 5 4 3)

      6. (< 5 6 7 8 2)

      7. (+ 5 (* 4 3))

      8. (/ (* 5 4 3) 12)

      9. (+ (sqrt 25) (- 15 14))

      10. (+ (expt 3 2) (expt 2 3) (sqrt 64))

      11. (define X 5) (+ (* X X) (- X 1))

      12. (define Y 2) (- (expt 4 Y) (expt Y 4))

      13. (first (list "Manny" "Moe" "Jack"))

      14. (rest (list "Larry" "Moe" "Curly")) ; Does your answer have parentheses? It matters!

      15. (length (list "Nina" "Pinta" "Santa Maria"))

      16. (length (list 2 2 2 2 2))

      17. (first (list "Hello"))

      18. (rest (list "Hello"))

      19. (+ 5 (length (list "Bubbles" "Blossom" "Buttercup")))

      20. (> (length (list "Abbot" "Costello"))
           (length (list "Laurel" "and" "Hardy")))

      21. (first (rest (list "UCI" "UCLA" "UCB" "UCSD")))

      22. (rest (rest (list "UCSC" "UCSF" "UCSB" "UCD")))

    2. How many items are in each of these lists? (The first four answers are given; they show you that when we ask this question, we're asking about the top-level list, not about how many items may be included in the internal, nested lists.) You can check your answers in DrScheme using length (but of course you should figure it out yourself first). Partners should alternate answering as before.

      1. (list 1975 1988 1992 2001) ; Answer: 4

      2. (list) ; Answer: 0 (this was the empty list)

      3. (list (+ 5 7) (+ 6 6) (+ 10 2)) ; Answer: 3

      4. (list 405 605 (list 10 210 710)) ; Answer: 3 (the third element is itself a list)

      5. (list "Hello" "Goodbye" (list "Wait a minute" "where" "am" "I"))

      6. (list (list 3 5 7 9) (list 1 2 3 4 5) 6)

      7. (rest (list "Hello"))

      8. (rest (list 2 3 5 7 11 13 17))

      9. (define L (list "UCI" "UCLA" "UCSD"))
        (list (rest L) (first L) (rest (rest L)))

      10. (list (list "UCI" "UCLA" "UCSD")) ; This one's tricky

      11. (define L (list "UCI" "UCLA" "UCSD")) (list L)

      12. (rest (rest (list 23 (list 34 25 17 19 45))))

  4. Write a function called height, which computes the height that a rocket reaches in a given amount of time. If the rocket accelerates at a constant rate g, it reaches a speed of g · t in t time units and a height of 1/2 · v · t where v is the speed at t. [from HTDP, ex. 3.3.4]

    1. The hardest thing about this (and the hardest thing about most programming) is not the technical details of the programming language but instead understanding the problem itself. Read it carefully.

    2. Here's a little guidance if you need it: First ask yourself what the input(s) (argument(s)) are, according to the description above. Then ask what the output (return value) is (i.e., what the formula is that computes it). If the formula for the output requires that you compute some intermediate value, then nest that intermediate formula in the output expression where necessary.

    3. Here's a little more guidance if the previous paragraph didn't help. It's important to understand the meanings of technical terms, because in the sciences we use these terms to describe precisely what we're talking about. Communication is vital, and part of that is speaking the same language. So make sure you're clear on the meanings of each of the terms in the paragraph; that should help you apply them to solving the problem. Of course the TAs are available to explain any terms.

  5. Conditional expressions

    1. Define a function called third that takes as its input a list of at least three items and returns the third item on the list. Remember to write a few examples first, showing the call to the function, its arguments, and the result you expect.

      1. Write the function definition, type it into DrScheme, and test it using your examples (and any other tests you like).

      2. Next, rewrite third to check whether its argument has at least three items. If it does, your function will return the same value as before. But if it doesn't, the function should return the empty list (which is just called empty). You will want to use cond.

    2. Write a function in Scheme that takes as input a person's age (in years) and returns one of three values, depending on the age: "Can neither vote nor drink", "Can vote but not drink", or "Can both vote and drink". Here, "drink" is shorthand for "legally purchase alcoholic beverages." Take care with this specification: This is a function that takes one argument (a number) and returns a double-quoted string.

      1. Before you run your code, write down on paper what you expect this function to return for the values 0, 15 through 25, and 100.

      2. Test your code with each of those inputs, correcting any discrepancies you find.

      3. When your function is working, show it to one of the TAs so they can check it out.