On this page:
1 Built-in structures
2 Animating invaders
3 List abbreviations
8.12

Lecture 14: Built-in structures🔗

This assignment is due on Tuesday, February 20 at 11:59pm. Submit it using Handin as assignment lecture14.

1 Built-in structures🔗

Rebecca Heineman is a game developer, hacker, and author who won the first e-sports championship in 1980. You can watch the game Space Invaders being played, or even play it yourself (use the arrow keys to move, and Alt or Option to fire).

The code written in the video 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.

Exercise 1. Define 3 examples of ListOfInvaders that did not appear in the video above. Then, explain in a comment why
(cons (make-posn 12 34)
      (make-posn 56 78))
is not a ListOfInvaders.

Exercise 2. Calculate step-by-step:
    (posn-y (first (rest (cons (make-posn 12 34)
                               (cons (make-posn 56 78)
                                     empty)))))
; = ???

Exercise 3. Write the data definition for a ListOfNumbers. Then, define 2 non-empty examples of ListOfNumbers. Why is (cons 1234 5678) not a ListOfNumbers?

2 Animating invaders🔗

The code written in the video 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.

Exercise 4. Start with these definitions:
(require 2htdp/image)
(define background (rectangle 200 300 "solid" "black"))
 
; A Posn is (make-posn Number Number)
; A ListOfPosns is one of:
; - empty
; - (cons Posn ListOfPosns)
Finish designing at least one of the following two functions. You’ll need to write more examples. (Feel free to design a helper function to process each Posn. Also feel free to define constants to make your examples and definitions clearer and shorter.)
; draw-invaders : ListOfPosns -> Image
; draws invader markers at the given positions
; (define (draw-invaders ps) ...)
(check-expect (draw-invaders empty) background)
 
; move-invaders : ListOfPosns -> ListOfPosns
; increase each Y coordinate by 1
; (define (move-invaders ps) ...)
(check-expect (move-invaders empty) empty)

The code written in the video 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.

Exercise 5. Examine the data definition of NaturalNumber in the video above. How many cases does it have? Then examine the template for processing a NaturalNumber in the video above. How many cases does it have? What do these numbers have in common? Why should they?

3 List abbreviations🔗

The code written in the video 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.

Exercise 6. Write the following without list abbreviations.
(define expand1
  (list "apple" "banana" "chestnut"))
 
(define expand2
  (list (list "apple" "banana") (list "chestnut")))
 
(define expand3
  (cons (list 1 2) (list 3 4)))
 
(define expand4
  (list (list)))
Write the following with list abbreviations.
(define abbreviate1
  (cons "apple"
        (cons "banana"
              (cons "chestnut" empty))))
 
(define abbreviate2
  (cons (cons "apple" empty)
        (cons (cons "banana" (cons "chestnut" empty))
              empty)))
 
(define abbreviate3
  (cons (list 1 2) (list 3 4)))
 
(define abbreviate4
  (cons (cons empty empty) (cons empty empty)))
Hint: Calculate step-by-step, using the equations at the end of the video in both directions:
; (list A B C D) = (cons A (cons B (cons C (cons D empty))))
; (list A B C)   = (cons A (cons B (cons C empty)))
; (list A B)     = (cons A (cons B empty))
; (list A)       = (cons A empty)
; (list)         = empty

Optional: Read Chapters 8 and 9 of the textbook.