Problem Set 1

home work!

Programming Language BSL

Purpose The purpose of this problem set is to remind you of the pattern-matching skills that your elementary-school teachers tried to teach you and to get you to “play” with BSL and DrRacket.

Finger Exercises Run a program from the HtDP/2e Prologue. Modify it and watch how its behavior changes. HtDP/2e: exercises 1, 2, 3, 5, 7, 9, 10.

Solutions must be submitted to Blackboard by 7:00pm on Tuesday, January 20th.

The solutions to all of the following problems should be contained in one .rkt file. Comment out portions that are not code. Late homework is not accepted

image

Problem 1 A boy gets retained by a neighborhood association to mow the grass and trim hedges, collect dues, and do miscellaneous chores. Just to make sure he is around when needed, he gets 50 dollars a month (he doesn’t have to work for that). For every hour he works, he gets $9.75.
  • How much does he get if he works 5 hours? 10 hours? 25 hours? per month.

  • Make a table that shows the results. Use the Interactions area in DrRacket as a calculator.

  • Write a mathematical formula for calculating how much the boy earns if he works H hours.

  • Using your formula, find out how much the boy earns when a major event forces him to work for 100, 122, and 135 hours in a single month.

Problem 2 A clothing store is having a sale on its products. Customers will get 20% off of the most expensive item, 30% off of the second most expensive item and 50% off of the third most expensive item. No other discounts will be applied. The store needs a program that can compute the value of the discount applied to each customer.

Define the program total-discount. It consumes three numbers: the price of the most expensive item, the price of the second most expensive item (or zero if only one item is bought) and the price of the third most expensive item (or zero if only one or two items are bought) in that order. It produces the value of the discount.

Problem 3 Define the program meters->miles that converts meters/second to miles/hour. The function consumes a velocity in meters/second and produces the velocity in miles/hour. Look up the conversion factor to convert from meters/second to miles/hour.

Problem 4 In BSL, you can juxtapose strings just like you can add numbers:

> (string-append "hello" " " "world")

"hello world"

Develop the function greeting. It accepts a string and returns a greeting for a formal letter, prefixing the given name with "Dear " and concluding the string with a colon. Thus, (greeting "Professor") produces "Dear Professor:" as the result.

Problem 5 Develop a function called distance-traveled. The function consumes an initial velocity, v0, a final velocity, vt, (velocity units are ft/sec) and time, t, (in seconds). The function produces the distance traveled in feet at time t.

Some background information: we assume a constant acceleration. Acceleration, a, is calculated by finding the change in velocity divided by the time elapsed. Distance traveled, x, is found by:

x = v0t + 12at2

You should develop a helper function called acceleration which calculates a constant acceleration given an initial velocity, a final velocity and time elapsed.

Problem 6 Define the program draw-house which draws a house from a triangle, square and rectangle. The function consumes one number and draws the shapes and places them using values derived from this one number.

For instance, (draw-house 40) produces the image: house40

And (draw-house 100) produces the image: house100