Lecture 17: Local definitions🔗

This assignment is due on Sunday, March 3 at 11:59pm. Submit it using Handin as assignment lecture17.

Exercise 1. Design at least one of the following two functions.
  • Design a function between-20-50 that takes a list of numbers as input and returns the list of all the given numbers between 20 and 50. The built-in function <= will help.

  • Design a function all-contains-lorem that takes a list of strings as input and returns the list of all the given strings containing "lorem". The built-in function string-contains? will help.

Don’t forget that the first step of the design recipe is data definitions. The data definitions will guide you to write the examples and the templates.

Exercise 2. Here are two useful functions:
(define-struct none [])
; A MaybePosn is one of:
; - (make-none)
; - Posn
; move-bullet : MaybePosn -> MaybePosn
; update the bullet as time passes
(check-expect (move-bullet (make-none)) (make-none))
(check-expect (move-bullet (make-posn 70 50)) (make-posn 70 45))
(define (move-bullet b)
  (cond [(none? b) b]
        [(posn? b) (make-posn (posn-x b) (- (posn-y b) 5))]))
; A MaybeNumber is one of:
; - (make-none)
; - Number
; move-rocket : MaybeNumber -> MaybeNumber
; move the rocket up if it is flying
(check-expect (move-rocket (make-none)) (make-none))
(check-expect (move-rocket 123) 122)
(define (move-rocket r)
  (cond [(none? r) (make-none)]
        [else (- r 1)]))
Abstract from the data definitions MaybePosn and MaybeNumber to form a new data definition [Maybe X]. After you define Maybe, don’t forget to redefine MaybePosn and MaybeNumber using Maybe.

You don’t need to abstract from any functions, but when you are done, the signatures of move-bullet and move-rocket may as well be written as follows:
; move-bullet : [Maybe Posn] -> [Maybe Posn]
; move-rocket : [Maybe Number] -> [Maybe Number]

Exercise 3. Design another function that takes one input and returns a Boolean. If you were to give this function to select as the predicate, what would X be? Put your answer in your submission as a comment in the following format:
; Exercise 3
; If I were to give the function ??? to select as the predicate,
; X would be ???
The first ??? should name the function you designed. The second ??? should name a data definition—such as Image or [ListOf Posn], but not an Image or a [ListOf Posn] because X is not any particular image and not any particular list.

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.

Optional: Read Chapter 16 of the textbook.