Fundamentals of Computer Science I (CS151.01 2006F)
[Skip to Body]
Primary:
[Front Door]
[Syllabus]
[Glance]
[Search]

[Academic Honesty]
[Instructions]
Current:
[Outline]
[EBoard]
[Reading]
[Lab]
[Homework]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Projects]
[Readings]
Reference:
[Scheme Report (R5RS)]
[Scheme Reference]
[DrScheme Manual]
Related Courses:
[CSC151.02 2006F (Davis)]
[CSCS151 2005S (Stone)]
[CSC151 2003F (Rebelsky)]
[CSC153 2004S (Rebelsky)]
This lab is also available in PDF.
Summary: This lab provides practice with simple userdefined procedures.
snoc
a. Open the reading in a separate window or tab.
b. Start DrScheme and make sure that you're in Pretty Big
Scheme mode.
Copy the frac
procedure from
the reading on procedures
and make sure that it works as advertised.
;;; Procedure: ;;; frac ;;; Parameters: ;;; val, a number ;;; Purpose: ;;; Express val as a fraction. ;;; Produces: ;;; rat, a rational number. ;;; Preconditions: ;;; val cannot be complex. ;;; Postconditions: ;;; rat is exact. ;;; rat is approximately equal to val (within some unknown level ;;; of accuracy). ;;; rat is the ratio of two integers. (define frac (lambda (val) (/ (inexact>exact (numerator val)) (inexact>exact (denominator val)))))
Write a Scheme procedure (addtwo a)
that returns the sum
a+2.
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).
c. How would you use this procedure to determine the number of feet in
a 1000meter race? (And no, It depends on the number of runners
is not an acceptable answer.)
a. Define a procedure, (poly1 x)
, that corresponds to the
polynomial 5x^{2}  8x + 2.
b. Test your procedure on the values 0, 1, 2, 3, 4.
In homework 2, you wrote a pair of definitions for the roots of the quadratic polynomial ax^{2}+bx+c. The disadvantage of those definitions was that you had to redefine a, b, and c whenever you wanted to recompute roots. You can now make the root computations a procedure.
a. Write a procedure (quadraticroot a b c)
that finds one root of the following quadratic equation by using the
quadratic formula. (I don't care which root you find.)
ax^{2}+bx+c = 0
In case you've forgotten, the quadratic formula is
(b +/ sqrt(b^{2}  4ac))/2a
b. Test your procedure by computing
(quadraticroot 1 5 6) (quadraticroot 2 10 12) (quadraticroot 1 4 4).
c. Use algebra to check these answers.
d. What are (quadraticroot 1 0 1)
and
(quadraticroot 1 0 2)
?
Write a procedure, (swapfirsttwo 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,
> (swapfirsttwo (list 'a 'b 'c 'd 'e))
(b a c d e)
In this problem, assume that the list given to swapfirsttwo
has at least two elements; do not worry about the possibility that
swapfirsttwo
might be applied to numbers, symbols,
empty lists, or lists with only one element.
The volume of a sphere of radius r is 4/3 times pi times r^{3}.
The circumference of a sphere of radius r is 2 times pi times r.
a. Write a procedure, (spherevolume 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, (spherecirc>radius circ)
,
that 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 inches.
d. Use these procedures to compute the volume of a Chicagostyle softball, which has a circumference of 16 inches.
e. Compute the volumes of each kind of softball using centimeters instead of inches.
snoc
Define a procedure snoc
(
)
that takes two arguments, of which the second should be a list.
cons
backwardssnoc
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
.
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)
In a figureskating 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.
snoc
Write snoc
(exercise 8) in two different ways. (You
should have already written it one way; find another way.)
Implement (roundtonplaces val p)
,
which rounds val to p places after the decimal point.
Note that you may have described the steps for roundtonplaces
in the lab on numeric values.
Write a procedure, (quadraticroots a b
c)
which computes both roots of the quadratic
equation and returns them in a list.
ax^{2}+bx+c = 0
For example,
> (quadraticroots 3 5 2)
(1 2/3)
Tuesday, 5 September 2000 [Samuel A. Rebelsky]
http://www.math.grin.edu/~walker/courses/151.fa00/labprocedures.html
(by Henry Walker; dated 1 September 2000)
http://www.math.grin.edu/~stone/courses/scheme/spring2000/proceduredefinitions.xhtml
(by John Stone; dated 17 March 2000)
Wednesday, 31 January 2001 [Samuel A. Rebelsky]
frac
procedure from the
reading).
Sunday, 4 February 2001 [Samuel A. Rebelsky]
add2
to addtwo
).
http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2001S/Labs/procedures.html
.
Tuesday, 10 September 2002 [Samuel A. Rebelsky]
for those who finish earlysection.
Friday, 13 September 2002 [Samuel A. Rebelsky]
http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2002F/Labs/procedures.html
.
Friday, 24 January 2003 [Samuel A. Rebelsky]
frac
(so that students don't need to
visit the reading to find it).
http://www.cs.grinnell.edu/~rebelsky/Courses/CS153/2003S/Labs/procedures.html
.
Thursday, 11 September 2003 [Samuel A. Rebelsky]
Friday, 12 September 2003 [Samuel A. Rebelsky]
Sunday, 3 September 2006 [Samuel A. Rebelsky]
Wednesday, 6 September 2006 [Samuel A. Rebelsky]
http://www.cs.grinnell.edu/~rebelsky/Courses/CS153/2006F/Labs/procedures.html
.
[Skip to Body]
Primary:
[Front Door]
[Syllabus]
[Glance]
[Search]

[Academic Honesty]
[Instructions]
Current:
[Outline]
[EBoard]
[Reading]
[Lab]
[Homework]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Projects]
[Readings]
Reference:
[Scheme Report (R5RS)]
[Scheme Reference]
[DrScheme Manual]
Related Courses:
[CSC151.02 2006F (Davis)]
[CSCS151 2005S (Stone)]
[CSC151 2003F (Rebelsky)]
[CSC153 2004S (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 Thu Nov 30 21:43:07 2006.
The source to the document was last modified on Wed Sep 6 09:20:21 2006.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2006F/Labs/procedures.html
.
You may wish to validate this document's HTML ; ;
Samuel A. Rebelsky, rebelsky@grinnell.eduhttp://creativecommons.org/licenses/bync/2.5/
or send a letter to Creative Commons, 543 Howard Street, 5th Floor,
San Francisco, California, 94105, USA.