[Current] [News] [Glance] [Discussions] [Instructions] [Search] [Links] [Handouts] [Outlines] [Readings] [Labs] [Homeworks] [Quizzes] [Exams] [Examples] [Fall2000.01] [Spring2000]
Summary: This lab provides practice with simple user-defined procedures.
Held: Wednesday, September 6, 2000
You may want to glance over the corresponding reading before beginning this lab.
Write a Scheme procedure (add2 a)
that returns the sum
a+2.
One foot is equal to exactly 761/2500 meters. Define a procedure named feet->meters 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. Use this procedure to determine the number of meters in one mile (5280 feet).
a. Define a procedure, poly1
, that corresponds to the
polynomial 5x^{2} - 8x + 2.
b. Test your procedure on the values 0, 1, 2, 3, 4.
a. Write a procedure (quadratic-root a b c) that finds one root of a
quadratic equation
ax² + bx + c = 0 using the quadratic
formula. Use it to find a root of the above equation.
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)?
Write a procedure swap-first-two that interchanges the first two elements on a list, leaving the rest of the list unchanged. Thus,
> (swap-first-two '(a b c d e)) (b a c d e)
In this problem, assume that the list given to swap has at least two elements; do not worry about the possibility that swap might be applied to numbers, empty lists, or lists with only one element.
The volume of a sphere of radius r is 4/3 times pi times r^{3}.
a. Write a
procedure named sphere-volume
that takes as its argument the radius of a
sphere (in, say, centimeters) and returns its volume (in cubic
centimeters).
b. Use this procedure to compute the volume of a softball (radius: eight centimeters).
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 '(beta gamma delta)) (beta gamma delta alpha) > (snoc 1 (list 2 3 4 5 6)) (2 3 4 5 6 1) > (snoc 'first '()) (first)
Hint: There are two ways to define this procedure. One uses calls to reverse and cons; the other uses calls to append and list.
Given a nonempty list of elements (e.g., (a b c)),
write a procedure, rotate
, that
creates a new list with the original first element moved to the end .
For example,
> (rotate '(a b c)) (b c a) > (rotate '(1 2)) (2 1) > (rotate (rotate '(first second third fourth))) (third fourth first second)
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.)
Tuesday, 5 September 2000
http://www.math.grin.edu/~walker/courses/151.fa00/lab-procedures.html
(by Henry Walker; dated 1 September 2000)
http://www.math.grin.edu/~stone/courses/scheme/procedure-definitions.xhtml
(by John Stone; dated 17 March 2000)
[Current] [News] [Glance] [Discussions] [Instructions] [Search] [Links] [Handouts] [Outlines] [Readings] [Labs] [Homeworks] [Quizzes] [Exams] [Examples] [Fall2000.01] [Spring2000]
Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.
This page may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2000F/Labs/procedure-definitions.html
Source text last modified Wed Sep 13 10:50:27 2000.
This page generated on Wed Sep 13 10:50:32 2000 by Siteweaver. Validate this page's HTML.
Contact our webmaster at rebelsky@grinnell.edu