Problem Set 4
Background
Read Chapter 9 of the book, paying attention to the design recipe for lists which summarizes and rephrases the same thing we were discussing in class.
Make sure that every function has a signature, description, and test cases (as needed).
Essentially and
and or
for lists
Problem 1
Start with Exercise 140
from the course text. There are two functions to define here –
all-true
and one-true
– make sure you do them both.
Filtering lists
A common action on lists is filtering, where we take a list and return a subset of that list, only keeping the elements that satisfy some property. Here we’ll have two problems that are examples of filtering.
Problem 2
Exercise 169, where you
keep posn
s that match a criteria.
Problem 3
Write a function drop-empty-strings
that takes a list of strings and returns a list
containing the non-empty strings.
As examples, consider the following tests (which are almost certainly not sufficient – what about the empty list, for example?):
(check-expect
(drop-empty-strings (cons "Morris" (cons "" (cons "Minnesota" (cons "" (cons "USA" '()))))))
(cons "Morris" (cons "Minnesota" (cons "USA" '()))))
(check-expect
(drop-empty-strings (cons "a" (cons " " (cons "b" (cons "" (cons "c" '()))))))
(cons "a" (cons " " (cons "b" (cons "c" '())))))
Mapping over lists
Another common action is mapping, where we take a list and return a new list that is the result of applying the same operation to every item in the list. Here we’ll have two problem that are examples of mapping.
Problem 4
Exercise 170 from the textbook, where you replace all the area codes in a list of phone numbers with a new area code. (This is the kind of thing you’d need to do when you split an area code, as you’d need to give roughly half the numbers the new area code.)
Problem 5
Write a function map-length
that takes a list of strings and returns a list of
integers that are the lengths of the respective strings. E.g., (& again, these tests
are not comprehansive):
(check-expect
(map-length (cons "Morris" (cons "Minnesota" (cons "USA" '()))))
(cons 6 (cons 9 (cons 3 '()))))
Originally written by @elenam, with subsequent modifications by @NicMcPhee