Multiple-valued procedures

Exercise 1

Develop 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.)

Exercise 2

Develop 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.

Exercise 3

Develop a procedure explode that takes a list as its argument and returns all of the elements of that list (separately) as values.

> (explode (list 'a 'b 'c 'd))
a
b
c
d
> (explode null)   ; This expression has no values!

(Hint: Use apply.)

Exercise 4

Describe the procedure that is the value of the expression `(compose string-append explode)'.

Exercise 5

Using compose, define and test a predicate that takes a predicate pred and a list ls as arguments and returns #t if ls contains more elements that satisfy pred than elements that do not, #f otherwise.


This document is available on the World Wide Web as

http://www.cs.grinnell.edu/~stone/courses/scheme/labs/multiple-valued-procedures.xhtml

Validated as XHTML 1.1 by the World Wide Web Consortium Cascading Style Sheet validated by the World Wide Web Consortium

created March 17, 2000
last revised September 14, 2001

John David Stone (stone@cs.grinnell.edu) and Ben Gum (gum@cs.grinnell.edu)