Lecture 19: Using built-in abstractions
This assignment is due on Sunday, October 27 at 11:59pm. Submit it using Handin as assignment lecture19. You only need to submit the first 4 exercises.
1 Midterm advice
Read every word of instructions.
Define everything you use.
Solve old problems, including those on the first midterm and before. Students who don’t review that basic material are often surprised by how poorly they do on the second midterm.
Feel free to ask questions, like on Discord!
2 Combining built-in abstractions
- Answer in comments:
; In the signature of map, X=??? and Y=??? ; In the signature of filter, X=??? Copy the signatures of map and filter, then replace X and Y and X throughout by what you guessed.
Look at what you just replaced, and remember that your main goal is to turn a list of strings into a list of numbers. Which of these two signatures shows an input list of strings? Which of these two signatures shows an output list of numbers?
Exercise 2. Design a function lengths-without-e that takes a list of strings and returns a list of numbers, which are the lengths of those strings that do not contain the letter "e".
- Answer in comments:
; In the signature of map, X=??? and Y=??? ; In the signature of filter, X=??? Copy the signatures of map and filter, then replace X and Y and X throughout by what you guessed.
Look at what you just replaced, and remember that your main goal is to turn a list of strings into a list of numbers. Which of these two signatures shows an input list of strings? Which of these two signatures shows an output list of numbers?
Do not follow the template for processing a list. Instead, when you get to design recipe step 5, use map and filter.
3 foldr
The code written in the videos above is available for your reference. To download it, don’t use “Save Page As” or “Save As”; use “Save Link As” or “Download Linked File” in your Web browser. If you can’t find the command, try right-clicking or two-finger-tapping or long-pressing.
4 build-list
For the rest of this page, you don’t need to submit anything.
(build-list 10 sqr) (build-list 10 (lambda (x) x)) (build-list 10 add1) (build-list 10 (lambda (x) (+ 1 x))) (build-list 10 (lambda (x) 1)) (build-list 5 (lambda (x) (/ x 10)))
Hint: Study the signature and purpose of build-list in Figure 95 of the textbook. There, N means NaturalNumber. But what is X?
(check-expect (evens-first 4) (list 0 2 4 6)) (check-expect (evens-first 7) (list 0 2 4 6 8 10 12))
Hint: Study the signature and purpose of build-list in Figure 95 of the textbook. What is X? Before you define the helper function to pass to build-list, remember to write down its signature, purpose, and examples.
(build-list 10 (lambda (x) (cond [(= x 3) 1] [else 0]))) (build-list 10 (lambda (x) (cond [(= x 4) 1] [else 0]))) (build-list 10 (lambda (x) (= x 5)))
(check-expect (diagonal 3) (list (list 1 0 0) (list 0 1 0) (list 0 0 1))) (check-expect (diagonal 10) (list (list 1 0 0 0 0 0 0 0 0 0) (list 0 1 0 0 0 0 0 0 0 0) (list 0 0 1 0 0 0 0 0 0 0) (list 0 0 0 1 0 0 0 0 0 0) (list 0 0 0 0 1 0 0 0 0 0) (list 0 0 0 0 0 1 0 0 0 0) (list 0 0 0 0 0 0 1 0 0 0) (list 0 0 0 0 0 0 0 1 0 0) (list 0 0 0 0 0 0 0 0 1 0) (list 0 0 0 0 0 0 0 0 0 1)))
Hint: What is X?
5 More midterm review
Exercise 8. Recall the data definition of Mobile from Problem set 6: Unions and recursion. Design the function count-leaves which counts how many leaves are in a given Mobile.
Exercise 9. Design the function count-big-leaves which counts how many leaves of weight more than 10 are in a given Mobile.