Quiz 4, CS 257

Initials ______ user-id on aix.unm.edu ________________
your time's not wasted
believe what I say
your big investment's
gonna pay off someday
- Timbuk 3
  1. What values are printed when you type each of the following to Scheme? (2 points each)
    expressionresult
    (every count '(aye be c deeee)) 
    (keep (lambda (x) (< (count x) 4))
          '(the quick brown fox))
     
    (define (foo f w)
       (f w 'four w))

    (foo (lambda (x y z)
           (word y (+ (* 10 x) z)))
         1)
     
    (accumulate (lambda (x y) (word x '- y))
                (every (lambda (w) (word w w))
                       '(a be sea)))
     
    (sentence (if (< 1 0) '(bip) '(bop boop)) 'shazam) 

  2. Define a function ave that takes a sentence of words and numbers as its sole argument, and returns the average of the numbers in the given sentence. (10 points)

    Eg (ave '(7 roses 8 carnations and 10 tulips)) returns 8.3333. (10 points)

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

  3. Extra credit: Explain briefly in English (a) what foo does and (b) how it works.
    Then (c) demonstrate your understanding by saying what (foo 'a1b2c3d4e5 '6fgh7ijk8m) returns. Note that the definition is not recursive: foo does not call itself.
    This is really tricky, but rewarding.
    (5 points)
    (define (foo a b)
      ((lambda (f aa bb) (f f aa bb))
       (lambda (g aaa bbb)
         (cond ((empty? aaa) '())
    	   ((number? (first bbb))
    	    (sentence (first aaa)
    		      (g g (butfirst aaa) (butfirst bbb))))
    	   (else (g g (butfirst aaa) (butfirst bbb)))))
       a
       b))