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 behaviors changes. HtDP/2e: exercises 1, 2, 3, 5, 7, 9, 10.

This is the only problem set to be completed without a partner and handed in on paper. Solutions must be submitted to Prof. Razzaq's office, 310 WVH, before 7PM Thursday Sep. 17. You may put it under the door if she is not in. To repeat: note that this one homework is, exceptionally, not due at the usual midnight time---it's due at 7:00 PM.

image

Problem 1 The total cost of attending Northeastern University includes tuition, room and board, and "student fees" (plus travel, books, and miscellaneous personal expenses, but we’ll leave those out to keep things simple).

Write a "Beginner Student Language" (BSL) expression that computes the total cost of attending Northeastern for 1 semester.

(Hint: http://www.northeastern.edu/admissions/cost-financial-aid/tuition-fees/)

Assuming a student takes 4 classes a semester, there are two semesters a year, and each class meets 3 times a week for 13 weeks, write a BSL expression that computes the cost of each lecture (or, equivalently, what do you pay to sleep late and skip one lecture).

Problem 2 An elderly woman suffers from type II diabetes. She injects insulin based on measurements of her blood sugar level. If the blood sugar level is less than 110, she doesn’t need to inject any insulin at all. For a value of 110, she injects 1 unit of insulin. For every additional increase of 15 in her blood sugar level, she gets one additional unit of insulin. (Thus, for a blood-sugar level of 124, she gets 1 unit; for a blood-sugar level of 125, she gets 2 units.) Note that insulin is dispensed only in whole-unit (that is, integer) quantities.

How much insulin does she need to inject for a blood sugar level of 140? 180?

Make a table that shows the number of units of insulin injected for blood sugar values of 100, 110, 120, ..., 200. Create a formula for calculating the insulin injections. Use the formula to determine how many units of insulin the woman needs to inject for a blood sugar level of 290.

You may write your formula in high-school math notation, or as a program in the BSL language we are using in class.

Use DrRacket’s interaction area as a calculator.

Hint: You may wish to look up the floor, min and max functions in the Dr.Racket help desk.

Problem 3 A pool player throws a ball on a pool table, and it then happens to roll along the diagonal. Someone films the action and then measures the ball’s trip:

after t =

  

0

  

1

  

2

  

3

  

4

  

sec

it has traveled d =

  

13

  

33.75

  

96.0

  

199.75

  

345.0

  

cm

Develop a formula that calculates how far the ball gets in t seconds. Check the formula for the first five entries in the above table. If it doesn’t work, work harder. When it works, figure out how far the ball gets in 6s.

Hint As discussed in class, functions come with many representations: tables, graphs, rules (formula), and so on. Here you are given a (partial) table, and your task is to guess a rule that explains how far the ball gets in t seconds. One way to guess such formulas is to switch representations. Here you could go from a table to a graph:

image

This graph should remind you of the many graphs of quadratic functions you have seen, meaning you should guess the a and b in a formula like this:

(+ (* a t t) b)

Plug in different values of t into this formula and watch what happens. Then compare with the table.

Use DrRacket’s interaction area as a calculator.

Challenge Not graded for points The ball lands at (12,5) and happens to travel along the straight line from there to 0,0:

image

Given the formula, can you figure out the distance the ball travels along the long/short side of the table? Develop a formula.

Recall what sin and cos compute. Try to use those functions to develop a formula.

Problem 4 A teenage girl volunteers to take care of the recycling in her condo association. Some weeks the members of the association fill all five, large recycling cans, which must then be rolled down a slope of nearly 200 yards on Wednesday evenings and picked up on Thursday morning after the town has emptied them. During summers, however, the association sometimes doesn’t even fill one of these containers. So the trustees decide to make the girl the following offer:

They pay a base fee of $10 per week just for her to be around if she is needed and $4 per container that needs to be handled.

Develop a formula that determines how much she is paid per week.

You may write your formula in high-school math notation, or as a program in the BSL language we are using in class.

Use DrRacket’s interaction area as a calculator.

Problem 5 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 "Prof. Razzaq") produces "Dear Prof. Razzaq:" as the result.

Problem 6 In BSL, you can create all kinds of shapes:
> (require 2htdp/image)
> (star 12 "solid" "red")

image

Develop the function my-star. It accepts a color string and returns an image of a 5-cornered star of radius 12 of that color. For example, if you apply my-star to "red", it returns the above image.