Quiz 2 Key, CS 257

  1. Respectable plant-people of the planet Blorg have their names inscribed upon their trunks shortly after germination. When a purple Blorgian speaks someone's name, it adds a suffix "-OOK-OOK" to the name inscribed on their trunk. When a green Blorgian addresses someone, it adds a prefix "BLIP-" to their name if it starts with a "Z", adds the prefix "BLOP-" to their name if it starts with a "Q", and adds the honorific suffix "-ICHOR" if it ends with an "S".

    Define a function (BLORG-SPOKEN-NAME COLOR-OF-SPEAKER NAME-ON-TRUNK) which, given the color of the speaking Blorgian and the name inscribed on the trunk of the Blorgian being addressed, returns the name to be spoken.

    For example:
    Color of Speaking Blorgian Name on Trunk of Blorgian Being Addressed Name to be Spoken
    PURPLE ZACH ZACH-OOK-OOK
    GREEN ZACH BLIP-ZACH
    GREEN ZAQS BLIP-ZAQS-ICHOR
    GREEN QUUX BLOP-QUUX
    GREEN QUIS BLOP-QUIS-ICHOR
    GREEN JIMMY JIMMY
    PURPLE JIMMY JIMMY-OOK-OOK


    (define (blorg-spoken-name color name)
      (cond ((equal color 'purple)
             (word name '-ook-ook))
    	((equal color 'green)
    	 (word (cond ((equal? (first name) 'z) 'blip-)   
    		     ((equal? (first name) 'q) 'blop-)   
    		     (else ""))                          
    	       name                                      
    	       (if (equal? (last name) 's)
                       '-ichor
                       "")))))
    
    

  2. Define a function (ADD-UP SEN) that, given a sentence of numbers, returns the sum of all the numbers in the sentence. For instance, (ADD-UP '(93)) = 93, (ADD-UP '(1 3 5)) = 9, and (ADD-UP '()) = 0.
    (define (add-up sen)
      (if (empty? sen)
          0
          (+ (first sen)
             (add-up (rest sen)))))
    

  3. What values are printed when you type each of the following to Scheme?
    expressionresult
    (or #f #f 'aye 'bee)AYE
    (and #t #t 'aye 'bee)BEE
    (sentence 1 2)(1 2)
    '(sentence 1 2)(SENTENCE 1 2)
    (word 12 13)1213
    (first 98765)9
    (first '(98 76 5))98
    (butfirst '(98 76 5))(76 5)
    (quote (and 1 2))(AND 1 2)
    (not 'false)#f

  4. Rewrite the following using IF rather than COND.
    (define (utensil cuisine)
       (cond ((equal? cuisine 'chinese) 'chopsticks)
             ((equal? cuisine 'ethiopian) 'fingers)
             (else 'fork)))
    

    (define (utensil cuisine)
       (if (equal? cuisine 'chinese)
           'chopsticks
           (if (equal? cuisine 'ethiopian)
               'fingers
               'fork)))