On this page:
1 Midterm advice
2 Combining built-in abstractions
3 foldr
4 build-list
5 More midterm review
8.14

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🔗

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 Combining built-in abstractions🔗

Exercise 1. Design a function lengths->3 that takes a list of strings and returns a list of numbers, which are the lengths of those strings longer than 3 characters.

Do not follow the template for processing a list. Instead, when you get to design recipe step 5, use map and filter.
  1. Answer in comments:
    ; In the signature of map, X=??? and Y=???
    ; In the signature of filter, X=???

  2. Copy the signatures of map and filter, then replace X and Y and X throughout by what you guessed.

  3. 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".

Do not follow the template for processing a list. Instead, when you get to design recipe step 5, use map and filter.
  1. Answer in comments:
    ; In the signature of map, X=??? and Y=???
    ; In the signature of filter, X=???

  2. Copy the signatures of map and filter, then replace X and Y and X throughout by what you guessed.

  3. 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 3. Design a function whole-halves that takes a list of numbers and returns a list that contains just the even numbers, halved.

Do not follow the template for processing a list. Instead, when you get to design recipe step 5, use map and filter.

3 foldr🔗

Exercise 4. Remember the function draw-invaders from Lecture 14: Built-in structures? It takes a [ListOf Posn] and returns an Image. It draws a marker at each given location. Design it again, still named draw-invaders, but use foldr rather than following the function template for processing a list. Use any marker image you like and any background image you like. Hint: What is X and what is Y? Design a helper function that follows the template for processing a Posn.

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.

Exercise 5. 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 6. 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 7. 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) (= x 5)))

Extra fun. 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: 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.