Assignment 0, Due Sept 13, 1999

You need to write 18 small scheme programs. There is an extra credit section at the end -- but there will be no help on the extra-credit questions. Submission instructions are at the end of this page.
  1. (duple n x) returns a list containing n copies of x
     
    > (duple 2 3) 
    (3 3)
    
    > (duple 4 '(ho ho))
    ((ho ho) (ho ho) (ho ho) (ho ho))
    
    > (duple 0 'blah)
    ()
    	
  2. (invert lst) , wheref lst is a list of 2-lists (lists of length two), returns a list with each 2-list reversed.
    > (invert '((a 1) (a 2) (b 1) (b 2)))
    ((1 a) (2 a) (1 b) (2 b)))
    	
  3. (list-index s los) returns the zero based index of the first occurence of s in los , or -1 if there is no occurence of of s in los
    > (list-index 'c '(a b c d))
    2
    > (list-ref '(a b c) (list-index 'b '(a b c)))
    b
    	
  4. (vector-index s vos) returns the zero based index of the first occurence of s in vos , or -1 if there is no occurence of of s in vos
    > (vector-index 'c '#(a b c d))
    2
    > (vector-ref '#(a b c) (vector-index 'b '#(a b c)))
    b
    	
  5. (ribassoc a los v fail-value) returns the value in v that is associated with s , or fail-value if there is no associated value. If the first occurrence of s in los has index n , the value associated with s is the nth value in v. There is no associated value for s if s is not a member of los . Assume that los and v are the same length.
    > (ribassoc 'b '(a b c) '#(1 2 3) 'fail)
    2
    
    > (ribassoc 'c '(a b foo) '#(3 squiggle bar) 'fail)
    fail
    
    > (ribassoc 'i '(a i o i) '#(fx (fz) () (fm fr)) 'fail)
    (fz)
    	
  6. (filter-in p lst) , where p is a predicate, returns a list of those elements in lst that satisfy the predicate.
    > (filter-in number? '(a 2 (1 3) b 7))
    (2 7)
    
    > (filter-in symbol? '(a (b c) 17 foo))
    (a foo)
    	
  7. (product los1 los2) returns a list of 2-lists that represents the Cartesian product of los1 and los2 . The two lists may appear in any order.
    > (product '(a b c) '(x y))
    ((a x) (a y) (b x) (b y) (c x) (c y))
    	
  8. (swapper s1 s2 slst) returns a list the same as slst, but with all occurences of s1 replaced by s2 and all occurences of s2 replaced by s1.
    > (swapper 'a 'd '(a b c d))
    (d b c a)
    
    > (swapper 'x 'y '((x) y (z (x))))
    ((y) x (z (y)))
    	
  9. (rotate '(a b c d)) returns a list similar to los, except that the last element of los becomes the first in the returned list.
    > (rotate '(a b c d))
    (d a b c)
    
    > (rotate '(notmuch))
    (notmuch)
    
    > (rotate '())
    ()
    	
  10. (union s0 s1) that takes two lists of symbols without duplicates s0 and s1 and returns a new list containing all the symbols in s0 and s1 without duplicates, where order isn't important.
    > (union '(a b c) '(d e f))
    (a b c d e f)
    > (union '(a b c) '(b a d))
    (a b c d)     
    
    	
  11. (set? ls) that takes a list of symbols ls and returns #t if ls is a representation of a set: a list of symbols without duplicates.
    > (set? '(a b b c))
    #f
    > (set? '(a b c))
    #t
    	
  12. (intersection s0 s1) that takes two sets (lists of symbols without duplicates) s0 and s1, and returns a new set containing all elements that are in both s0 and s1.
    > (intersection '(a b c) '(b c d))
    (b c) or (c b)
    > (intersection '(a b c) '(d e f))
    ()
    
    	
These six are a bit harder.
  1. (down lst) wraps parentheses around each top-level element of lst
    > (down '(1 2 3))
    ((1) (2) (3))
    
    > (down '(a (more (complicated)) object))
    ((a) ((more (complicate))) (object))
    	
  2. (up lst) removes a pair of parentheses from each top-level element of lst . If a top-level element is not a list, it is included in the result as is. The value of (up (down lst)) is equivalent to lst but (down (up lst)) is not neccessarily lst
    > (up '((1 2) (3 4)))
    (1 2 3 4)
    
    > (up '((x (y)) z))
    (x (y) z)
    	
  3. (count-occurences s slst) returns the number of occurences of s in slst
    > (count-occurences 'x '((f x) y (((x z) x))))
    3
    > (count-occurences 'w '((f x) y (((x z) x))))
    0
    	
  4. (flatten ls) that takes a (possibly deep) list of symbols ls and returns a new flat list containing all the symbols of ls in the same order as they were previously; that is, flatten removes all of the inner parentheses from its argument.
    > (flatten '(a b c))
    (a b c)
    > (flatten '((a b) c (((d)) e)))
    (a b c d e)
    > (flatten '(a b (() (c))))
    (a b c)
    
  5. (merge lon1 lon2) where lon1 and lon2 are lists of numbers that are sorted in ascending order, returns a sorted list of all the numbers in lon1 and lon2
    > (merge '(1 4) '(1 2 8))
    (1 1 2 4 8)
    
    > (merge '(35 62 81 90 91) '(3 83 85 90))
    (3 35 62 81 83 85 90 90 91)
    	
  6. (prefixes ls) takes a list ls and returns a list of the prefixes of ls in no particular order.
    > (prefixes '(a b c))
    (() (a) (a b) (a b c))
    > (prefixes '(a (a b) c))
    (() (a) (a (a b)) (a (a b) c))
    > (prefixes '())
    (())
    	

Extra Credit

  1. (path n bst) , where n is a number and bst is a binary search tree that contains the number n, returns a list of L's and R's showing how to find the node containing n. If n is found at the root, it returns the empty list.
    > (path 17 '(14 (7 () (12 () ()))
                    (26 (20 (17 () ())
                            ())
                        (31 () ()))))
    (R L L)
    	
  2. (car&cdr s slst errvalue) returns an expression that when evaluated, produces the code for a procedure that takes a list with the same structure as slst and returns the value in the same position as the leftmost occurrence of s in slst. If s does not occur in slst , then errvalue is returned.
    > (car&cdr 'a '(a b c) 'fail)
    (lambda (lst) (car lst))
    
    > (car&cdr 'c '(a b c) 'fail)
    (lambda (lst) (car (cdr (cdr lst))))
    
    > (car&cdr 'dog '(cat lion (fish dog) pig) 'fail)
    (lambda (lst) (car (cdr (car (cdr (cdr lst))))))
    
    > (car&cdr 'a '(b c) 'fail)
    fail
    	

Turning in Assignments

  1. Submit a transcript of your scheme session showing the running of each of the functions on test data. Our test data and data that you generate. Use the (transcript-on) and (transcript-off) functions in scheme or cut and paste.

Here is an example submission session:

spider:413 tmp> ~cs326/bin/sub0 as0.ss
I am sushil
Done Copying...bye
spider:414 tmp> 
    
You should not have to change permissions on as0.ss or on your directory. For example, here's the result of typeing ls -al in the direcory containing as0.ss.
spider:414 tmp> ls -la
total 6
drwx------   2 sushil   staff        512 Aug 30 12:05 ./
drwxr-xr-x  16 sushil   staff        512 Aug 25 15:34 ../
-rw-------   1 sushil   staff        445 Aug 22 14:03 as0.ss
spider:415 tmp> 
    


Sushil Louis
Last modified: Thu Sep 2 14:07:27 PDT 1999