CSci 1301: Problem Set 4
Due: Monday, November 12th at 11:59pm
Please make sure that every function has a signature, description, and test
cases (as needed).
Problem 1
Read Chapter 9 of the book, paying attention to the design recipe for lists (it summarizes and rephrases the same thing we were discussing in class).
Then do the following problems, including the check-expect tests,
signatures, descriptions, and then the implementation:
- Exercise 140 (5 points).
-
A function that takes a list of strings and produces the sum of their lengths (5 points).
-
A function that takes a list of positions and another position (let's call it X) and produces the sum of distances from each of the positions on the list to X (5 points). Hint: write and use a helper
distance
function (think of what its signature would be).
Problem 2
-
Exercise
169 (5 points)
-
Exercise
170 (5 points)
-
Write a function that takes a list of strings and returns a list of their length (3 points).
-
Write a function that takes a list of strings and returns a list of all strings on that list that
don't start with a lower-case or upper-case "a" (3 points).
Problem 3
Read Section 11.3
in the textbook and do the following exercises:
-
Exercise 187 (4 points)
-
Exercise 188 (4 points)
-
Exercise 190 (write both
prefixes
and suffixes
functions) (9 points). Make sure to start with check-expects and a signature. Note
that the function needs a recursive helper function.
Don't forget to write signatures before you write the any code!
Clarification of the problem: There are two possible interpretations of "all prefixes":
- An empty list of letters is a prefix of every list of letters. Then the base case is an empty list of letters, and your check-expects would include:
(check-expect (prefixes (list "a" "b" "c")) (list empty (list "a") (list "a" "b") (list "a" "b" "c")))
(check-expect (prefixes (list "a")) (list empty (list "a")))
;; base case:
(check-expect (prefixes empty) (list empty))
-
The smallest list of letters (and the smallest prefix) is a one-letter list. Then your base case is when the list of letters
has exactly one element, and your check-expects include:
(check-expect (prefixes (list "a" "b" "c")) (list (list "a") (list "a" "b") (list "a" "b" "c")))
;; base case:
(check-expect (prefixes (list "a")) (list (list "a")))
Both approaches have their challenges, but can be solved by carefully thinking of a solution in terms of the recursive case
and the first element.
How to submit
Submit your file(s) via canvas, make sure to name them as required.
CSci 1301
course web site.