Homework #8


This problem requires Lecture 6 and book section 1.2.

Exercise 1.15, problems 1-5, p.24-25 - (60 minutes)
Here's a whole plate full of Scheme functions for you to define. You'll need lots of practice writing Scheme functions to get through this class. Here's a start. See Exercises 1.16 and 1.17 if you want more practice.

1. (duple n x) returns a list containing n copies of x.

	> (duple 2 3)
	(3 3)
	> (duple 4 '(ho ho))
	((ho ho) (ho ho) (ho ho) (ho ho))
	> (duple 0 '(blah))
	()

2. (invert lst), where lst is a list of 2-lists (lists of length two), returns a list with each 2-list reversed.

	>(invert '((a 1) (a 2) (b 1) (b 2)))
	((1 a) (2 a) (1 b) (2 b))
3. (filter-in pred lst) returns the list of those elements in lst that satisfy the predicate pred.
	> (filter-in number? '(a 2 (1 3) b 7))
	(2 7)
	> (filter-in symbol? '(a (b c) 17 foo))
	(a foo)

4. (every? pred lst) returns #f if any element of lst fails to satisfy pred, and returns #t otherwise.

	> (every? number? ' (a b c d 3 e))
	#f
	> (every? number? '(1 2 3 5 4))
	#t
Notice: Uninitialized string offset: 1 in /users/home2/ta/cs330ta/public_html/cs330/inc/codeformat.php on line 150

5. (exists? pred lst) returns #t if any element of lst satisfies pred, and returns #f otherwise.

	> (exists? number? '(a b c 3 e))
	#t
	> (exists? number? '(a b c d e))
	#f
Notice: Uninitialized string offset: 1 in /users/home2/ta/cs330ta/public_html/cs330/inc/codeformat.php on line 150

YOU MUST NAME YOUR FUNCTIONS WITH THE SAME NAMES THEY HAVE IN THE BOOK (i.e., duple, invert, filter-in, every?, and exists?).

Debugging Tip: DrScheme has a trace facility that will display when a function is called, what its parameters were, and what its return value was. The syntax for turning on tracing for a particular function is (trace <function>). So, for example, you could turn on debugging for your duple function by typing (trace duple). You have to explicitly turn on tracing for each function you want to trace. The (untrace <function>) operator will likewise turn tracing off for a particular function.


You must log in first before submitting homework assignments.

Instructions on submitting homework is here


Last updated at 7:07 am on Friday, September 17, 2004.