8.12

### Lecture 8: StructuresðŸ”—

This assignment is due on Tuesday, January 30 at 11:59pm. Submit it using Handin as assignment lecture8.

Exercise 1. Copy the code below into your Definitions Window.
 ; Exercise 1 ; The line below is a ; A Posn is (make-posn Number Number) ; The line below is a ; make-posn : Number Number -> Posn ; The line below is a ; posn-x : Posn -> Number ; The line below is a ; posn-y : Posn -> Number ; The line below is a (define here (make-posn 30 50)) ; The line below is a (define there (make-posn 70 45))
At the end of each line “; The line below is a”,
write one of the following phrases:
• constant definition

• variable definition

• function definition

• data definition

• signature

Don’t change anything else you copy.

Exercise 2. Copy the expressions below into your Definitions Window. What are their results? Try to predict the results before asking DrRacket.
 ; Exercise 2 ;   (posn-x there) ; = (posn-x (make-posn 70 45)) ; = ??? ;   (posn-y here) ; = ??? ; = ...

Exercise 3. Copy the expressions below into your Definitions Window. What are their results? Try to predict the results before asking DrRacket. (If it runs into an error, write “error” without quotes at the end.)
 ; Exercise 3 ;   (+ 1 (posn-y there)) ; = (+ 1 (posn-y (make-posn 70 45))) ; = ??? ; = ... ;   (+ 1 there) ; = ??? ; = ... ;   (- (posn-x there) (posn-x here)) ; = ??? ; = ... ;   (make-posn (posn-x there) (posn-y here)) ; = ??? ; = ... ;   (posn-y (posn-x here)) ; = ??? ; = ...

Exercise 4. Design a function called size that takes a Posn as input and computes the sum of the absolute values of its x and y. For example, the size of (make-posn 2 -3) is 5.

Remember to follow the design recipe whenever you design or write a function. In particular, every type mentioned in a signature must be introduced by a data definition, except for these well-known types: Number, Image, String, Color, Boolean, Posn.

Exercise 5. Design a function called normalize that takes a Posn as input and produces a new Posn:
• The produced x should be the given x divided by the size of the given Posn.

• The produced y should be the given y divided by the size of the given Posn.

For example, because the size of (make-posn 2 -3) is 5, the result of
 (normalize (make-posn 2 -3))
should be (make-posn 0.4 -0.6). Here 0.4 is 2 divided by 5, and -0.6 is -3 divided by 5.