FOOP Midterm #1

SOLUTIONS IN RED

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))

-2


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

125

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

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))

37

E. (g (f 30))
38

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.

(define (bingo n)
  (cond [(between? n 1 15) (word 'B n)]
        [(between? n 16 30) (word 'I n)]
        [(between? n 31 45) (word 'N n)]
        [(between? n 46 60) (word 'G n)]
        [(between? n 61 75) (word 'O n)]))

If you test this code (and you should!), note that the outputs are in double-quotes. The current version of Simply Scheme as run in DrRacket puts double-quotes around words that contain any capital letter. Don't worry about that.

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!

(define (fire-in-direction x1 y1 x2 y2)
  (se (- x2 x1) (- y2 y1)))

 

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?)
(define (query sent)
  (se (first (bf sent))
      (first sent)
      (bf (bf (bl sent)))
      (word (last sent) '?)))

Note that the order of the bf and bl functions in (bf (bf (bl sent))) could be done in any order as long as there are two bfs and one bl.