CSci 1301: Problem Set 5
Due: Wednesday, November 30th at 11:59pm by email
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
checkexpect. Write your tests before you write a function (but feel
free to add more after it's been written).
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)
Exercise 250.
Write checkexpect
s that represent the functionality you
were abstracting over, and at least one checkexpect
that
demonstrates different functionality.
Problem 2: Abstract functions II (15 points)
Important: do not use max
or min
for these functions. Note that your code may look slightly inefficient; that's ok.

Write a recursive function that finds the smallest number in a nonempty list
of numbers (your base case should be a oneelement list: it returns the only element in the list).
Make sure to write checkexpects first.

Write a recursive function that finds the largest number in a nonempty list
of numbers (your base case should be a oneelement list: it returns the only element in the list).
Make sure to write checkexpects first.

Write a recursive function that finds that shortest string in a nonempty list
of strings (your base case should be a oneelement list: it returns the only element in the list).
Make sure to write checkexpects first.

Write a general (abstract) function that consumes a list and a comparison function (i.e. a function on two elements that returns #true or #false, depending on what element is "larger"),
and returns the "minimum" element of the list according to that comparison. Write the checkexpects
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
checkexpects 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 checkexpects first; make sure you are giving the function the most general signature.
Problem 4: Using predefined functions (8 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 20 squares (images), in which the first one is 1by1 pixel, the second one is 2by2, and so on (hint: use buildlist)
 Use a predefined functions to sort a list of strings by length (shortest first)
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.
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.