Fundamentals of CS I (CS151 2002F)

User-Defined Procedures

Summary: This lab provides practice with simple user-defined procedures.

You may want to glance over the corresponding reading before beginning this lab.

Exercises

Exercise 0: Preparation

Start DrScheme and make sure that you're in full Scheme mode.

Exercise 1: Values as Fractions

Copy the frac procedure from the reading on procedures and make sure that it works as advertised.

Exercise 2: Adding 2

Write a Scheme procedure (addtwo a) that returns the sum a+2.

Exercise 3: Converting Feet to Meters

a. Define a Scheme procedure, (feet->meters ft) that takes one argument, a real number representing a length measured in feet, and returns the number that represents the same length as measured in meters. Note that one foot is equal to exactly 761/2500 meters.

b. Use this procedure to determine the number of meters in one mile (5280 feet).

Exercise 4: A Quadratic Polynomial

a. Define a procedure, (poly1 x), that corresponds to the polynomial 5x2 - 8x + 2.

b. Test your procedure on the values 0, 1, 2, 3, 4.

Exercise 5: Quadratic Roots

a. Write a procedure (quadratic-root a b c) that finds one root of a quadratic equation
ax2 + bx + c = 0 using the quadratic formula. Use it to find a root of the above equation. (I don't care which root you find.)

b. Test your procedure by computing

(quadratic-root 1 -5 6)
(quadratic-root 2 -10 12)
(quadratic-root 1 4 4).

c. Use algebra to check these answers.

d. What are (quadratic-root 1 0 1) and (quadratic-root 1 0 2)?

Exercise 6: Swapping List Elements

Write a procedure, (swap-first-two lst), that, given a list as an argument, creates a new list that interchanges the first two elements of the original list, leaving the rest of the list unchanged. Thus,

> (swap-first-two (list 'a 'b 'c 'd 'e))
(b a c d e)

In this problem, assume that the list given to swap-first-two has at least two elements; do not worry about the possibility that swap-first-two might be applied to numbers, empty lists, or lists with only one element.

Exercise 7: Spherical Calculations

The volume of a sphere of radius r is 4/3 times pi times r3.

The circumference of a sphere of radius r is 2 times pi times r.

a. Write a procedure named (sphere-volume r) that takes as its argument the radius of a sphere (in, say, centimeters) and returns its volume (in, say, cubic centimeters).

b. Write a procedure named (sphere-circ->radius circ), which converts the circumference of a sphere to its radius.

c. Use these procedures to compute the volume of a standard softball, which has a circumference of 12".

d. Use these procedures to compute the volume of a Chicago-style softball, which has a circumference of 16".

e. Compute the volumes of each kind of softball using centimeters instead of inches.

Exercise 8: snoc

Define a procedure snoc (``cons backwards'') that takes two arguments, of which the second should be a list. snoc should return a list just like its second argument, except that the first argument has been added at the right end:

> (snoc 'alpha (list 'beta 'gamma 'delta))
(beta gamma delta alpha)
> (snoc 1 (list 2 3 4 5 6))
(2 3 4 5 6 1)
> (snoc 'first null)
(first)

Hint: There are at least two ways to define this procedure. One uses calls to reverse and cons; the other uses calls to append and list.

Exercise 9: Rotate

Write a procedure, (rotate lst), that, given a nonempty list of elements (e.g., (a b c)), creates a new list with the original first element moved to the end .

For example,

> (rotate (list 'a 'b 'c))
(b c a)
> (rotate (list 1 2))
(2 1)
> (rotate (rotate '(first second third fourth)))
(third fourth first second)

Exercise 10: Scoring Figure Skating

In a figure-skating competition, judges have observed the competitors' performances and awarded three separate scores to each competitor: one for accuracy, one for style, and one for the difficulty of the chosen routine. Each score is in the range from 0 to 10. The rules of the competition specify that a competitor's three scores are to be combined into a weighted average, in which accuracy counts three times as much as difficulty and style counts twice as much as difficulty. The overall result should be a single number in the range from 0 to 10.

a. Write a comment in which you describe the nature and purpose of a procedure that takes three arguments -- a competitor's accuracy, style, and difficulty scores -- and returns their weighted average.

b. Define the procedure that you have described.

c. Test your procedure, looking for cases in which the weighted average is computed incorrectly. (If you find any, make corrections in your definition.)

If you find that you have extra time, you might want to attempt the following tasks:

 

History

Tuesday, 5 September 2000 [Samuel A. Rebelsky]

Wednesday, 31 January 2001 [Samuel A. Rebelsky]

Sunday, 4 February 2001 [Samuel A. Rebelsky]

Tuesday, 10 September 2002 [Samuel A. Rebelsky]

Friday, 13 September 2002 [Samuel A. Rebelsky]

 

Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Mon Dec 2 09:19:12 2002.
The source to the document was last modified on Fri Sep 13 08:36:15 2002.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2002F/Labs/procedures.html.

You may wish to validate this document's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu