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

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.

  1. 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.

  2. 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.

  3. 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.
  4. Exercise 2.20 (page 104). (And remember what you learn in this exercise! You'll use it later in this course.)
  5. Exercise 2.23 (page 107). Call your procedure my-for-each.
  6. Do exercises 2.24, 2.25, and 2.26 (page 110), but don't turn in the answers.
  7. Exercise 2.27 (page 110). Call your procedure my-deep-reverse.
  8. 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?.
  9. Define a procedure every? which takes two formal parameters: (every? pred seq) evaluates to #t when every element of seq satisfies pred, and evaluates to #f otherwise.
  10. Define a procedure any? which takes two formal parameters: (any? pred seq) evaluates to #t when at least one element of seq satisfies pred, and evaluates to #f otherwise.
  11. Exercise 2.59 (page 153). Call your procedure unordered-union-set.
  12. Exercise 2.62 (page 155). Call your procedure ordered-union-set.
  13. 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.
  14. 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.