Lecture 19: Follow the template
This assignment is due on Sunday, October 23 at 11:59pm. Submit it using Handin as assignment lecture19. You only need to submit the first 2 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 Pre-landing check
Check your answer using “Show answer” above. Did you get it right? If not, click the button again to get another set, and keeping doing it until you get it right without peeking at the answer.
You don’t need to submit anything for this check.
3 Follow the template and make a wish
Exercise 1. To review what we’ve learned, let’s design a function our-sort to sort a list of numbers into ascending order. Do steps 1–4 of the design recipe: write the template meticulously (named our-sort), but don’t fill it in.
Exercise 2. We wish we could just fill in the template by replacing the dots with the name of a helper function. Design the function insert which takes a Number and a ListOfNumbers and puts the number in the correct place in the list, assuming the list is already sorted. The new number should go before all the numbers bigger than it, and after all the numbers smaller than it. Don’t use the built-in sort function.
4 More midterm review
For the rest of this page, you don’t need to submit anything.
Exercise 3. Recall the data definition of Mobile from Problem set 6: Unions and recursion. Design the function count-leaves which counts how many leaves a Mobile has.
Exercise 4. Design the function count-big-leaves which counts how many leaves of weight more than 10 a Mobile has.
4.1 Filter
Exercise 5. Design a function remove-multiples-of-10 that removes every number divisible by 10 from a list of numbers. Use filter.
Hint: Study the signature and purpose of filter in Figure 95 of the textbook. What is X?
Exercise 6. Design a function has-multiple? that takes a list of numbers and a number as inputs, and checks whether any number in the given list is a multiple of the given number. Use ormap.
Hint: Study the signature and purpose of ormap in Figure 95 of the textbook. What is X?
4.2 Map
Exercise 7. Design a function move-invaders which takes a list of Posns and increases each Y coordinate by 1. Use map.
Hint: Study the signature and purpose of map in Figure 95 of the textbook. What is X and what is Y?
(check-expect (prefix-all "Thanks, " (list "Daniel" "Alicia")) (list "Thanks, Daniel" "Thanks, Alicia"))
4.3 Fold
Exercise 9. Design a function count which accepts a list of strings and uses foldr to count the number of items in the list. You may not use the built-in length function in this exercise. Instead, design a helper function and give it to foldr, which will perform the counting.
Hint 1: Study the signature and purpose of foldr in Figure 96 of the textbook. What is X and what is Y? Before you define the helper function, remember to write down its signature, purpose, and examples.
Hint 2: When foldr is used to sum together a list of numbers, the
helper function + gets two inputs—
Exercise 10. Abstract your count function to work with a list of any data type.
4.4 build-list
(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) (cond [(= x 5) 1] [else 0])))
(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: Study the signature and purpose of build-list in Figure 95 of the textbook. What is X? Before you define each helper function to pass to build-list, remember to write down its signature, purpose, and examples.