Cluster 5 * Summer 2004
Computer Science Through Games and Scheme Programming
David G. Kay, Dan Frost, Kevin Papke
Scheme Lab Activities: Second Set
-
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.
-
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.
-
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.
-
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.
-
(* 3 4 5)
-
(- 10 2 3)
-
(> 25 12)
-
(<= 35 34)
-
(> 5 4 3)
-
(< 5 6 7 8 2)
-
(+ 5 (* 4 3))
-
(/ (* 5 4 3) 12)
-
(+ (sqrt 25) (- 15 14))
-
(+ (expt 3 2) (expt 2 3)
(sqrt 64))
-
(define X 5) (+ (* X X)
(- X 1))
-
(define Y 2) (- (expt 4
Y) (expt Y 4))
-
(first (list "Manny"
"Moe" "Jack"))
-
(rest (list "Larry"
"Moe" "Curly"))
; Does your answer have parentheses? It matters!
-
(length (list "Nina"
"Pinta" "Santa Maria"))
-
(length (list 2 2 2 2 2))
-
(first (list "Hello"))
-
(rest (list "Hello"))
-
(+ 5 (length (list "Bubbles"
"Blossom" "Buttercup")))
-
(> (length (list "Abbot"
"Costello"))
(length (list "Laurel" "and"
"Hardy")))
-
(first (rest (list "UCI"
"UCLA" "UCB" "UCSD")))
-
(rest (rest (list "UCSC"
"UCSF" "UCSB" "UCD")))
-
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.
-
(list 1975 1988 1992 2001)
; Answer: 4
-
(list)
; Answer: 0 (this was the empty list)
-
(list (+ 5 7) (+ 6 6) (+
10 2)) ; Answer: 3
-
(list 405 605 (list 10 210
710)) ; Answer: 3 (the third element
is itself a list)
-
(list "Hello"
"Goodbye" (list "Wait a minute" "where" "am"
"I"))
-
(list (list 3 5 7 9) (list
1 2 3 4 5) 6)
-
(rest (list "Hello"))
-
(rest (list 2 3 5 7 11 13
17))
-
(define L (list "UCI"
"UCLA" "UCSD"))
(list (rest L) (first L) (rest (rest L)))
-
(list (list "UCI"
"UCLA" "UCSD"))
; This one's tricky
-
(define L (list "UCI"
"UCLA" "UCSD")) (list L)
-
(rest (rest (list 23 (list
34 25 17 19 45))))
-
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]
-
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.
-
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.
-
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.
-
Conditional expressions
-
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.
-
Write the function definition, type it into
DrScheme, and test it using your examples (and any other tests you like).
-
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.
-
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.
-
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.
-
Test your code with each of those inputs,
correcting any discrepancies you find.
-
When your function is working, show it to
one of the TAs so they can check it out.