CS 131 Homework 1

Due by the beginning of class Monday, January 30
  1. Read pages 1-27 of the textbook.

  2. Write the function (member? x lst), which takes an arbitrary value and a list of values and returns #t if the value is equal? to any value in the list, or #f otherwise. Examples:

    (member? 'c '(a b c d)) => #t
    (member? 'apple '(banana cherry orange)) => #f
    (member? 'anything '()) => #f
    

  3. Rewrite member? using and, or, and not instead of if or cond. The and and or special forms take any number of argument expressions, which are evaluated in order, and return #t or #f as soon as a final value can be determined. The not function takes a single boolean expression and returns its logical negation. Examples:

    (and (= (+ 2 2) 4) (> 10 3)) => #t
    (and (= (+ 2 2) 4) (< 10 3)) => #f
    (or (= (+ 2 2) 5) (equal? 'cat 'dog) (> 2 1) (+ 'this 'will 'crash)) => #t
    (not (= (+ 2 2) 5)) => #t
    

  4. Write the function (union set1 set2), which takes two sets of values, each represented as a list containing no duplicates, and returns a list representing the union of the sets. The order of the elements does not matter. Examples:

    (union '(a b c) '(b c d e))  =>  (a b c d e)  [or some other ordering]
    (union '() '(b c d e)) => (b c d e)
    (union '(a b c) '(c a b)) => (a b c)
    

  5. In the definition of combos from class, replace the call to append with a call to your union function and test out combos to make sure it works.

  6. Do parts 1-5, 8, and 9 of Exercise 1.15 on pages 24-25. (Write duple, invert, filter-in, every?, exists?, product, and down.)

  7. Write the function (every-other lst), which takes a list and returns a list of every other element, starting with the first. You may assume that lst contains at least one element. Examples:

    (every-other '(a b c d e f)) => (a c e)
    (every-other (cdr '(a b c d e f))) => (b d f)
    (every-other '(a b) => (a)
    

  8. Write the function (powerset set), which takes a set of values and returns its power set, that is, the set of all subsets of set. The order of the elements does not matter. Hint: map may come in handy here. Example:

    (powerset '(a b c)) => (() (a) (b) (c) (a b) (a c) (b c) (a b c))  [or some other ordering]
    

Turning in your homework

Put all twelve of your function definitions into a single Scheme file called assign1.ss and submit this file electronically by running the following script from any Pomona Linux machine:

/common/cs/submit/cs131-submit

You must run this script from the directory that contains your Scheme file. You will be prompted for the assignment name (in this case, hw01) and then the name of your file. In general, cs131-submit will allow you to submit any number of files, one at a time, but for this assignment you only need to submit one file. More information about cs131-submit is available here.

For convenience, you might want to add an alias to your .bashrc file as follows:

alias cs131-submit='/common/cs/submit/cs131-submit'

If you have questions about anything, don't hesitate to ask!