CS 450 Homework 2
Carl Offner
Fall 2009
Due Friday, September 25, 5:00 PM
As will be the case for all future assignments, I will collect all work
electronically at 5:00 PM on the day it is due. So be sure it is in the proper
directory at that time.
Put your answers in file ASanswers.scm in your new
hw2 directory .../cs450/hw2, with discussion when
required as Scheme comments. (A Scheme comment starts with a
semicolon and continues to the end of the line.) I will load and test
your file, so be sure that
- It's bug free.
- Anything (such as an explanation that you
might write) that is not Scheme code is commented, so the Scheme
interpreter does not attempt to evaluate it.
In particular, anything that isn't yet working should be a stub or a
comment.
The purpose of the following problems is to become familiar with the
quote special form, and to learn to think recursively about lists and
to use the list primitives of Scheme: cons, car and
cdr.
Write a Scheme procedure is-list? that tests to see if
an object is or is not a list, according to the definition of a list
in the Scheme language definition. (Where is this definition? OK,
I'll tell you: it's on page 25, in Section 6.3.2, which is titled
"Pairs and lists". It's the second paragraph in that section.)
Note: UMB Scheme already contains a built-in function list?
that does this, and is written so cleverly that it works on cyclic
data structures. Please write your own procedure, however; you may
assume that the argument passed to it is not cyclic.
- Exercise 2.55 (page 145). Also answer the following question:
why does
(car 'abracadabra)
not evaluate to
quote
Let me give you a word of warning about this problem, as well as other
problems of this sort: I find that many students taking this class are
confused about what a definition is. So for instance in class, I gave you a
definition of the "quote" special form. I have found in the past that a lot of
students don't take this definition very seriously. They come up with their
own understanding of this special form, which sometimes works and sometimes
doesn't. The point of the definition that I gave in class was to tell you
exactly how that special form works. If you are not using that definition in
your answer, then almost certainly your answer is incorrect.
- Exercise 2.18 (page 103). Call your procedure
my-reverse. Now that you know about quote you can
test with more than lists of integers.
- Exercise 2.20 (page 104). (And remember what you learn in this
exercise! You'll use it later in this course.)
- Exercise 2.23 (page 107). Call your procedure
my-for-each.
- Do exercises 2.24, 2.25, and 2.26 (page 110), but don't turn in
the answers.
- Exercise 2.27 (page 110). Call your procedure
my-deep-reverse.
- Exercise 2.54 (page 145), with the following change: in the
statement of the problem, substitute eqv? for eq?.
This makes the definition closer to the Scheme language
definition. (In fact, it would be even better to substitute "pair"
for "list" in the recursive definition given in the problem. You can
do this.) Call your procedure my-equal?.
- Define a procedure every? which takes two
formal parameters:
- pred is a predicate, and
- seq (i.e., "sequence") is a list.
(every? pred seq) evaluates to #t when every element of
seq satisfies pred, and evaluates to #f otherwise.
- Define a procedure any? which takes two
formal parameters:
- pred is a predicate, and
- seq (i.e., "sequence") is a list.
(any? pred seq) evaluates to #t when at least one element of
seq satisfies pred, and evaluates to #f otherwise.
- Exercise 2.59 (page 153). Call your procedure
unordered-union-set.
- Exercise 2.62 (page 155). Call your procedure
ordered-union-set.
- Define a procedure remove-val which takes two
parameters: a value and a list. It returns a list which is the same
as the original list with the first element of the given value
deleted. If there is no such element, the original list is returned.
So for instance,
(remove-val 3 '(4 5)) ==> (4 5)
(remove-val 3 '(2 3 4 3)) ==> (2 4 3)
This exercise will also be useful to you in a later assignment.
- Suppose you are given two sets stored as "unordered lists"—that is,
the order of the elements in the list is not significant, and maybe there is no
natural way to order the elements anyway. Using only what we have done in
class, how could you tell if two sets were identical? (Remember that the order
of the elements in the list representing the set does not matter.) You should
not write any code for this problem. Just say in clear English how to solve
this problem. It's very simple.
And further, this problem will be of use to you in a later assignment.