CSE 413 Spring 2000

Assignment 2 -- More Scheme

Due: at the beginning of lecture on Wednesday, April 12, 2000

All of these problems should be done without using side effects (i.e. redefining variables or using set!).  Be sure to test your functions on various cases, including empty lists, simple lists with no sublists, complex lists, etc.

Part I.  Written (paper) problems

  1. Given the following top-level definitions,
    (define a 21)
    
    (define b 100)
    
    (define (clam a) (- a b))
    
    (define (squid b) (clam b) )
    What are the values of the following scheme expressions?

    1.  (let ( (b 12)
              (c (+ b 7)) )
              
            (* a b c)  )
    2.  (let* ( (b 12)
               (c (+ b 7)) )
              
             (* a b c)  )
    3.  (let* ( (a (+ b a))
               (b 12)
               (c (+ b a)) )
              
             (* a b c)  )
    4. (squid 11)
  2. Suppose we have the following definitions:
    (define w 42)
    (define x 17)
    (define y (lambda (n) (+ n w)))
    (define z (lambda (x) (+ x 2)))
    What are the values of the following expressions?

    1. (map (lambda (x) (z x)) '(1 2 3 4))
    2. (let ( (w (map y '(1 2 3 4)))) w)
  3. For each of the following functions indicate whether it is tail recursive or not tail recursive. Why or why not?

    1. (define (sumlist x)
         (if (null? x) 
             0
             (+ (car x) (sumlist (cdr x)))))
    2. (define (positive-numbers x)
         (cond ((null? x) ())
               ((> (car x) 0) (cons (car x) (positive-numbers (cdr x))))
               (else (positive-numbers (cdr x)))))
    3. (define (r2 x) 
         (let ((r (reverse x)))
              (append r r)))
    4. (define (same-length x y)
         (cond ((and (null? x) (null? y)) #t)
                ((null? x) #f)
                ((null? y) #f)
                (else (same-length (cdr x) (cdr y)))))

Part II. Programming Problems.

Write and test Scheme functions to solve the following problems.  You should save all of your function definitions in a single source file.  In DrScheme you can use  File->Save definitions as  to create a file that contains your function definitions.  The normal convention is to use the extension .scm to end Scheme file names.

You will be asked to turn in both the function definitions themselves and a printout showing a Scheme session where you test the functions.  Your test cases should be sufficient to show that the functions work correctly (i.e., works on both empty and non-empty lists, etc.)  In DrScheme, you can use File->Print Interactions to print the session transcript.

  1. Define a function (deep-map op lst) that applies function op to list lst. deep-map returns an object with the same "shape" as lst, with each element being the result of applying op to the corresponding element of the original component of  lst. If elements of lst are themselves lists, they should be examined recursively and op should be applied to those elements.   Do NOT use the Scheme function map in your implementation.  NOTE: a function applied to the empty list should return the empty list. Examples:
    (define (double x) (* 2 x))
    (deep-map double '(1 (2) 3 4 () ))  => (2 (4) 6 8 () )
    (deep-map number? '(() ((3 g) a) 34 (g 4) ))) => (() ((#t #f) #f) #t (#f #t))
  2.  
    1. Define a function deep-double that uses deep-map to apply the function double (see definition of double in the previous example) to every item in its argument:
      (deep-double '(1 (2) 3 4 ))  => (2 (4) 6 8)
      (deep-double '(((1) 2) 3))) => (((2) 4) 6)
    2. Define a function deep-dbl to do the same thing using a lambda expression instead of  the named double function.

  3. Define a function (zip op x y) that takes lists (x1 x2 x3 ....) and (y1 y2 y3 ...) and returns the list ((op x1 y1) (op x2 y2) (op x3 y3) ...).  If either list argument x or y is empty, zip should return an empty list.   Examples:
  4. (zip + '(1 2 3 4) '(5 10 15 20)) => (6 12 18 24)
    (zip list '(a b c d) '(6 7 8 9)) => ((a 6) (b 7) (c 8) (d 9))
  5. Define a function (3fold op id x y), where op is a function, id is the identity element for that function, x is the list (x1 x2 x3 ...) and y is the list (y1 y2 y3 ...).  Execution of (3fold op id x y) should return the list
    (op x1 y1 (op x2 y2 (op x3 y3 (...(op xn yn id))))
    In other words, 3fold reduces the two lists to a single value by applying function op to pairs of values taken from x and y and the result of recursively evaluating 3fold on the tails of the lists.  The identity id is the value of function 3fold for the base case where either list is empty. Examples:
  6. (3fold + 0 '(1 2 3) '(10 15 20)) => 51
    (3fold * 1 '(-1 2 -3) '(2 -10 -3)) => 360
    (3fold list () '(a b c) '(d e f)) => (a d (b e (c f ())))
    (3fold - 0 '(10 5 3) '(5 10 10)) => 3

 What to Hand In

For this (as well as most) assignments, you should turn in your work both electronically over the web and on paper.

Electronic Submission

Turn in a copy of your Scheme source file from part II using this turnin form.

Paper Submission

Hand in the following:
  1. Written answers to the problems from Part I.
  2. A printout of the receipt you received from the online submission of the file containing your Scheme functions from Part II.  This listing will include a copy of your Scheme code.
  3. A printout showing the transcript of the Scheme session demonstrating the results produced by your functions for suitable test cases.  This should contain enough to show that your functions work, but no more than necessary to do that. (Don't worry if there are some minor typos in the transcript - just draw a line through anything we should ignore.)