Assignment 4
Tests for exercises 8, 9 and 10: Friday 2/2 at 9:00pm
Full solutions: Tuesday 2/6 at 9:00pm
Purpose To practice designing functions on lists.
Exercise 1 HtDP Exercise 110
Exercise 2 HtDP Exercise 115
Exercise 3 HtDP Exercise 130
Exercise 4 Design the function du, which consumes a list of strings and computes their total length.
Hint: Remember string-length.
Graded Exercises
Exercise 5 Picture frames
Design a function that takes a list of images and frames them all. That is, it turns this:
into this:
Exercise 6 Image areas
Design a function that can take a list of images and produce a number representing their total area. (Remember: a picture’s area depends only on its size, not on its contents. The circle and square in the images above have the same width and height, and so have the same area, despite appearing to be different shapes. The triangle and star have the same height as the other shapes, but different widths; they definitely have different areas.)
Exercise 7 Trailing off...
Design a function that, for example, can turn this input:
into this example output:
A longer list would produce something like
Specifically, the function should take a list of images and place them beside each other, aligned at the bottoms, and such that the composite images are shrunk by some scale factor (here I used 0.9). There’s nothing special about the images themselves; the example images above used the answer to the Picture Frames exercise above, so you can easily see the sizes of the component images.
Exercise 8 Design a function eliminate, that takes a List of Numbers lon and a Number n, and constructs a new List of Numbers with all the same values as lon except for any numbers equal to n. For instance,
(check-expect (eliminate (cons 5 (cons 4 (cons 5 (cons 2 '())))) 5) (cons 4 (cons 2 '()))) Note: this is essentially remove-all, simplified to lists of only numbers. We have not addressed how equal? works yet, so the full flexibility of remove-all doesn’t yet make sense. Also – do not use remove-all to implement this function!
Challenge (optional): Design a function no-dups, that takes a List of Numbers, and constructs a new list with any duplicate values removed (and keeping just a single one of the copies). For example,
Exercise 9 We know that cons allows us to add a number to the front of a list of numbers. Design its opposite, snoc, that allows us to add a number onto the end of a list of numbers.
Exercise 10 Design a function search-for-string that takes a string and a list of strings, and returns a list containing all the strings in the given list that contain the given string as a substring.
Hint: There are other string-processing functions besides substring; one of them is very useful for this problem. Look up the documentation for substring, and scroll up through the documentation to find a function that might help with checking string containment...