Lecture 14: Built-in structures
This assignment is due on Tuesday, October 4 at 11:59pm. Submit it using Handin as assignment lecture14.
1 Built-in structures
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 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
(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)
; 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)
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
(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)))
(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)))
; (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.