CS 30 Homework 1

Due by the beginning of class Tuesday, September 10

Finish reading The Little Schemer Chapters 1-3. Then write and test the following Scheme functions. Remember to think recursively. As a reminder, here is the approach we discussed in class today for solving a problem recursively:

Solving a problem recursively means using the problem solution itself as part of the solution.
  1. (copies n x) takes a number n and any value x and returns a list containing n copies of x. Examples:

  2. (count a lat) takes an atom a and a list of atoms lat and returns the number of times a appears in lat. Examples:

  3. (rac ls) takes a non-empty list ls and returns the last item in the list (rac is the "mirror image" of car). Examples:

  4. (rdc ls) takes a non-empty list ls and returns all of the items except the last (rdc is the "mirror image" of cdr). Examples:

  5. (snoc x ls) takes any value x and a list ls, and returns a new list with x added to the end of ls (snoc is the "mirror image" of cons). Examples:

  6. (reverse lat) takes a list of atoms lat and returns a new list with all of the atoms in reverse order. Hint: use snoc in your definition of reverse. Examples:

  7. (seconds lats) takes a list of lats and returns a new lat consisting of the second atom from each lat in the list. Examples:

  8. (double a ls) takes an atom a and a list ls and doubles the first a in ls (it is the converse of rember, which removes the first a). Examples:


Turning in your homework

Put all of your function definitions into a single file called hw1.scm and bring a hardcopy printout of the file to class with you on Tuesday.