Fundamentals of CS I (CS151 2002F)

Procedures That Return Multiple Values

Exercises

Exercise 0: Preparation

a. Scan through the reading on procedures that return multiple values. Make sure you understand the standard Scheme procedures values and call-with-values.

b. Start DrScheme.

Exercise 1: Testing mixed-number-parts

What are the results of applying mixed-number-parts to each of the following values? Explain each result.

Exercise 2: Computing with Multiple Results

a. What do you expect to happen when you add 2 to the result of (mixed-number parts 7/5)? Try it and see.

b. What do you expect to happen when you take the car of the result of (mixed-number parts 7/5)? Try it and see.

Exercise 3: Separating A Number's Parts

Using call-with-values and mixed-number-parts, write (whole-part rat), which gives the whole part of a rational number, and (frac-part rat), which gives the fractional part.

Exercise 4: Divide

Write a non-recursive version of the (divide dividend divisor) procedure mentioned in the reading. Your procedure should return two values, a quotient, quot, and a remainder, rem. You should ensure that

You can use Scheme's built-in division operation or the quotient and remainder procedures in writing your answer.

Exercise 5: Tallying By Parity

Define and test a procedure tallies-by-parity that takes any list of integers as its argument and returns two values, the number of even integers in the list and the number of odd integers in the list.

Hint: Use tail recursion.

You may not use partition in your solution.

a. Solve the problem without using call-with-values.

b. Solve the problem with call-with-values.

For Those With Extra Time

Extra 1: Separating Association Lists

Define and test a recursive procedure that takes an association list als as argument and returns two results: a list of the keys of als, and a list of the values of als.

 

History

Fall 2000 [Samuel A. Rebelsky]

Friday, 13 April 2001 [Samuel A. Rebelsky]

Thursday, 14 November 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:06 2002.
The source to the document was last modified on Thu Nov 14 14:54:45 2002.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2002F/Labs/multivalue-proc.html.

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

Samuel A. Rebelsky, rebelsky@grinnell.edu