On this page:
1 Midterm advice
2 Pre-landing check
3 Follow the template and make a wish
4 More midterm review
4.1 Filter
4.2 Map
4.3 Fold
4.4 build-list
8.6.0.14

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

Our second midterm is Tuesday night! The advice from the first midterm is still valid:
  • 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

Are you ready? Click the button

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?

Exercise 8. Design a function prefix-all which takes a string and a list of strings, and puts the given string in front of every string in the given list.
(check-expect (prefix-all "Thanks, " (list "Daniel" "Alicia"))
              (list "Thanks, Daniel" "Thanks, Alicia"))
Use map.

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—the first element of a list, and the sum of the rest of the list—and returns the sum of the entire list. When foldr is used to concatenate together a list of strings, the helper function string-append gets two inputs—the first element of a list, and the concatenation of the rest of the list—and returns the concatenation of the entire list. Similarly, when you use foldr to find the length of a list, your helper function will get two inputs—the first element of a list, and the length of the rest of the list—and should return the length of the entire list. In each instance, the result (sum or concatenation or length) of the rest of the list is already computed, just like when you are filling in a list-processing template.

Exercise 10. Abstract your count function to work with a list of any data type.

4.4 build-list

Exercise 11. Anticipate (then determine) the values of the following expressions.
(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?

Exercise 12. Design a function evens-first that takes a natural number as input and returns the list of the first that many even numbers starting with 0.
(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.

Exercise 13. Anticipate (then determine) the values of the following expressions.
(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])))

Exercise 14. Design a function diagonal that receives one input, a number, and returns a list of that many lists of 0 and 1 in the following diagonal arrangement:
(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.