CSci 1301: Problem Set 6
Due: Monday, November 23rd at 11:59pm by e-mail
You are required to write signatures and descriptions for all
functions, and also descriptions of all your structures. Additionally,
functions, except the world problems, must be tested using
check-expect. Write your tests before you write a function (but feel
free to add more after it's been writen).
The quality of your tests will
be graded.
Make sure to have enough test cases to
demonstrate that the function is correct.
I may take
points off for a lack of testing (in addition to those taken off for incorrect behavior)
if your function doesn't work on
particular expected data and there is no test for it. It's much better
to leave a test that fails (perhaps in comments) that shows that the function
doesn't work on given data and you don't know how to fix it.
Problem 1: Abstract functions I (5 points)
Exercises 238
in section
17.1. For every abstract function
write check-expect
s that represent the functionality you
were abstracting over, and at least one check-expect
that
demonstrates different functionality.
Problem 2: Abstract functions II (15 points)
-
Write a recursive function that finds the smallest number in a non-empty list
of numbers (your base case shoudl be a one-element list: it returns the only element in the list).
Make sure to write check-expects first.
-
Write a recursive function that finds the largest number in a non-empty list
of numbers (your base case shoudl be a one-element list: it returns the only element in the list).
Make sure to write check-expects first.
-
Write a recursive function that finds that shortest string in a non-empty list
of strings (your base case shoudl be a one-element list: it returns the only element in the list).
Make sure to write check-expects first.
-
Write a general (abstract) function that consumes a list and a comparison function,
and returns the "minimum" element of the list according to that comparison. Write the check-expects
first. The first example (with the smallest number) must work when < and a list of numbers are passed to the function,
and the second example must work when > and a list of numbers are passed. Think carefully what you need to pass
to make the third example above work. You may write a helper function, or use a
lambda
.
Make sure you write the most general signature for the abstract function (in particular, check that your signature
works for the third example above).
Write one example of using the abstract function that is not listed above.
Problem 3: Abstract functions III (12 points)
-
Write a function that consumes a list of numbers and returns the first odd number on the list. Make sure to write
check-expects first.
-
Write another function that follows the same pattern: takes a list and returns the first element that
satisfies some condition.
-
Generalize the functions by adding a parameter for a predicate, such as
odd?
.
Write check-expects first; make sure you are giving the function the most general signature.
Problem 4: Using predefined functions (10 points)
For this exercise you need to use predefined functions in Section 18. You may not write your own recursive functions.
- Use predefined functions to add all of the x coordinates in a list of positions
- Use perdefined functions to select all strings that do not start with an "a" or "A" in a list of strings
- Use predefined functions to create a list of circles, given a list of non-negative numbers that
are used as the circles radiuses
- Use predefined functions to create a list of 20 squares, in which the first one is 1-by-1 pixel, the second one is 2-by-2, and so on (hint: use build-list)
- Use a predefined functions to sort a list of strings by length
CSci 1301
course web site.
The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.