FOOP Midterm #1

0. (0 points) NAME___________________________________ PERIOD _______

INSTRUCTIONS

1. (5 points; each subproblem 1 point apiece) Assume that the following defines have been done:

(define x 12)
(define y 5)
(define (square x) (* x x))
(define (between? a b c)
  (and (>= a b) (<= a c)))

What will be printed for each expression, below? (If the expression will produce an error, write ERROR.)

A. (define (thing a b c)
     (cond ((> a b) (* 2 c))
           ((between? a b c) (+ a 2))
           (else (- a b))))
   (thing y (- x y) (square y))


B. ((if se word sentence) x y)


C. (let ((z (square y))
        (w (square x)))
     (sqrt (+ z w)))
		

For D and E, use the definitions of f and g, below.

(define (f x) (- (* 2 x) 3))
(define (g x) (* (/ x 3) 2))

D. (f (g 30))

E. (g (f 30))

2. (5 points) In the game BINGO, the numbers 1 through 75 are used and there are letters that correspond to the numbers as shown in this table:
Number Range Letter
1--15
B
16--30
I
31--45
N
46--60
G
61--75
O

The function bingo takes a number as its input and returns a word containing a letter and the number as follows:

> (bingo 74)
O74
> (bingo 37)
N37
> (bingo 3)
B3
> (bingo 16)
I16
> (bingo 60)
G60

Write the function bingo. You can use the helper function between? from problem #1.

 

3. (5 points) Katniss Everdeen finds herself in the Arena, which is a geometric plane. She has her bow and arrows, but she cannot see her target due to darkness. You, however, can see the whole Arena, including her coordinates and a monster's coordinates. Katniss is the world's greatest archer, so if you tell her the monster's position relative to hers, she will be able to shoot it without fail. (Katniss has to keep moving between shots because of the games maker.)

Katniss has a mobile device to which you can send a sentence, telling her how far in the x and y directions she should shoot an arrow.

You need to write the function fire-in-direction which takes four inputs: Katniss's (x, y) coordinates and a monster's (x, y) coordinates. fire-in-direction works as follows:

> (fire-in-direction 0 0 5 4)       ; Katniss at (0, 0), monster at (5, 4)
(5 4)
> (fire-in-direction 1 3 9 6)       ; Katniss at (1, 3), monster at (9, 6)
(8 3)
> (fire-in-direction -5 7 -13 -2)   ; Katniss at (-5, 7), monster at (-13, -2)
(-8 -9)

Every time a monster appears in the Arena, fire-in-direction will automatically be called and the sentence it produces will immediately be sent to Katniss. Write fire-in-direction and save her!

 

 

4. (5 points) Problem 5.21. Write a procedure query that turns a statement into a question by swapping the first two words and adding a question mark to the last word:

> (query '(you are experienced))
(are you experienced?)  

> (query '(i should have known better)) 
(should i have known better?)