Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

Homework 4

Programming Language BSL

Due Date: Saturday February 4, 9pm

Purpose The purpose of this problem set is to internalize design skills. Topically, the problem set

focuses on functions that process structures and arbitrary unions of data.

Finger Exercises HtDP/2e: 68, 69, 70, 71, 79. Do not submit your solutions to these; they are

optional "warmup" problems to help you get comfortable with the current material and don’t get

graded. 

Problem 1 Consider the following structure definitions:

(define-struct lecture-hall [number capacity])

(define-struct automobile [year make model])

(define-struct football-player [name position number])

(define-struct shirt [material size color])

1. What are the names of the constructors and the selectors that each of the structures adds to Racket?

2. Provide data definitions for the structure definitions above. Make appropriate assumptions about what data goes with which field.

3. Develop templates for functions that consume the structures above.

Problem 2 A movie store sells two kinds of movies: regular and classic. For regular movies, the store

tracks the product id (a string, such as "234-87-1DX"), its base price, and the number of years it has

been in the collection. For each year a movie is in stock, it is marked down by 3.5% of the base price,

but no movie is sold for less than $2. For classic movies, they track the product id (again,

represented as a string), and its price, which is never discounted.

Design a data representation for the store’s items and a function that computes the current price of

an item.

Hint: the math formula that discounts a price p by 3.5% a year for y years is p x (1 - 0.035)y. (That

is, multiply the price times 0.965 raised to the y power.)

Problem 3

Here is a data definition, Vec, for two-dimensional vectors represented as posn structs with numeric

fields, plus a little function that lets you add two vectors together.

  ; A Vec is a (make-posn Number Number)


(define (vec+ v w)


(make-posn (+ (posn-x v) (posn-x w))

(+ (posn-y v) (posn-y w))))


   Shivers and Vesely were in a hurry and just sort of slapped this code together in a haphazard,

sloppy way. It’s apalling, but true: they left out several elements of the Design Recipe for the vec+

function. Please fill in the missing purpose statement, signature and some tests.

  Please design a function, random-vec, that takes four integers for inputs, xlo, xhi, ylo, and yhi,

and returns a Vec where the x coordinate is a random number in the range [xlo,xhi) and the y

coordinate is similarly in the range [ylo,yhi). You will want, of course, to read up on the random

function in the Dr. Racket help desk.

You will need to be a little thoughtful about tests/examples for this function. Hint: you might

consider looking up the check-random function in Racket’s help desk. Or you could consider

checking properties of random-vec’s output that are not affected by its random choices.

Let’s design some simple code elements that might be useful in writing a video game. Here are some

structure and data definitions that we could use:


(define-struct ship [size]) (define-struct ufo  [size])

; A GamePiece is one of: ; - (make-ship Number)  ; - (make-ufo  Number)

; A NASA space ship, with a given size

; An alien UFO, with a given size


  Design a function, piece->image, which takes a GamePiece and produces an image for it. You

may use any rendering you like, as long as (1) UFOs and space ships look different, and (2) the

size of the result is given by the piece’s specified size field. For example, you could make a space

ship be a red square, and a UFO be a blue circle, to pick one simple rendering.

  Design a function, piece+scene, that takes three arguments: a game piece gp, a location loc

(which is a Vec), and a background scene scene (which is an Image). The function adds an

image of the given game piece to the background scene at the given location.

Here is a data definition for game pieces that move across the screen.

(define-struct movable [gp loc vel])    ; A moving game piece


; A Movable is a (make-movable GamePiece Vec Vec)

;

; Interpretation: A "movable" is a game piece with a location and a velocity, 

; where the last two things are both 2D vectors. Velocity is specified       

; in units of pixels-per-clock-tick, in computer-graphics coordinates.       

; So a velocity of (make-posn 3 -4) means that every clock tick, the piece   

; moves right three pixels, and up four pixels (up, not down, because this   

; is computer-graphics coordinates).


Please design the following functions:

 movable+scene takes two inputs, a Movable and a movie scene (an Image). It adds an image of

the Movable to the scene at its current location (which is part of the Movable’s information),

producing the resulting scene.

movable->scene takes a Movable and produces an image showing it painted onto some blank-

background scene BG that you define with

(define BG-WIDTH  300)



(define BG-HEIGHT 300)

(define BG (empty-scene BG-WIDTH BG-HEIGHT))


(You can alter the width and height constants if you like; the particular values you use aren’t

critical.)

next-movable takes a Movable that describes a game piece at some point in time, and returns a

Movable that describes the game piece one clock tick later. In other words, it moves the Movable

– one step, as specified by its current location and velocity.

  Obviously, we are setting things up to use big-bang, where the state of the world is a single

Movable value, flying across the screen. Write a big-bang expression that will run a movie

showing some moving UFO or space ship (of your choice) flying across the screen.

Extra credit: Things would be more interesting if we added a "key handler" to our big-bang

expression so our animation could be responsive to keyboard input.

Design a function, maybe-new-movable, that takes two inputs: a Movable (which describes the

state of the world) and a "key event," which is a string that describes a key pressed by the human

interacting with the system. (For example, if the player presses the x key, the key event will be the

string "x"; if the player presses the up-arrow key, the key event would be the string "up". You

can read more about key events in the Racket help desk, if you want.)

If the string is "x", then the function should return a new Movable which is a space ship, sitting at

the center of the screen, moving with a random velocity vector (say, both x and y components are

random numbers in the range [-10,10]). (You may wish to take advantage of earlier random code,

here...)

If the string is "y", then the function should similarly return a new Movable that represents a

UFO positioned at the center of the screen, with a random velocity.

If the string is anything else, then the function should return the Movable that was passed in as its

first argument.

Your movie is now interactive! Run it with maybe-new-movable installed as the on-key handler

by adding this clause to your big-bang:

  [on-key maybe-new-movable]

P.S. From here, of course, you can hack away to your heart’s content – change the physics, the

look of the objects, add more keyboard inputs, whatever you like. But be careful to segregate this

extra code in a separate file and don’t submit it for grading.