Midterm Exam - CS257 Spring 1999


CIRT user-id:________________@unm.edu

Every attempt to employ mathematical methods in the study of chemical questions must be considered profoundly irrational and contrary to the spirit of chemistry ... If mathematical analysis should ever hold a prominent place in chemistry -- an aberration which is happily almost impossible -- it would occasion a rapid and widespread degeneration of that science.
Auguste Comte, Philosophie Positive, Paris, 1838

  1. Consider the following definition:
    (define foo
      (lambda (n)
        (cond ((= n 0) 3)
              ((= n 1) 1)
              (else (+ (foo (- n 2)) (foo (- n 1)))))))
    Fill in the five missing elements in the right hand column. (2 points each)

    n(foo n)

  2. What values are printed when you type each of the following to MzScheme? (2 points each)

    (or (member 'b '(a b c))
        (member 'z '(x y z z y)))
    (map (repeat cdr 2) '((w h y) (t o e) (h i s)))
    (accumulate (lambda (x y) (append y '(*) x))
    	    '((over) (buckle shoe) (one two)))
    (let ((x '(a b c)))
      (cond ((number? x) x)
            (x => cadr)
    	(else (list x x x))))
    (let ((a 1))
      (let ((a 2)
    	(b (+ a 3)))
        (list 'a a 'b b)))


  3. Define a higher-order function foo which has the following behavior. (5 points)
    (foo (lambda (x) (list x x)))    ; ((aye aye) (bee bee))
    (foo (lambda (x) 'blah))         ; (blah blah)
    (foo number?)                    ; (#f #f)

  4. Given this definition:
    (define funky
      (lambda (f x y)
        (cond ((null? x) x)
              ((pair? x)
    	   (cons (funky f (car x) (car y))
    	         (funky f (cdr x) (cdr y))))
              (else (f x y)))))
    What will each of the following return? (2 points each)
    (funky + '(1 (2 3) 4) '(10 (11 12) 13))
    (funky list '((a b) c d) '((e f) g h))
    (funky <= '(1 2 (10 4) 40) '(2 1 (20 20) 20))
    (funky (lambda (g x) (g x))
           (list - (list list number?) (lambda (x) (* x 2)))
           '(2 (3 4) 5))
    Write a brief English description of funky that explains its purpose. At a high level! (I.e. don't explain how it works. Your explanation should be suitable for someone who wants to know when to use it, but doesn't care how it was implemented.) (5 points)

  5. Extra Credit: what will this call to foo return? (5 points)
    (define foo
      (lambda (g h s r)
        (if (null? s)
            (r g s (foo h g (cdr s) r)))))
    (foo car
         (lambda (x) (accumulate (lambda (a b) b) x))
         '((g o o d) (l o t t o) (t o d a y) (s w a m i) (t r i u m p h e d))
         (lambda (f s x)
           (cons (f (car s)) x)))