On this page:
From Structures to Lists
Worlds and Posns
Color Posns
6.10.1

Lab 5: More Fun with Recursion

Important Note Whenever we say to "design a function", we mean that you need to follow the design recipe. Any other time that you write a function in this class, you also need to follow the design recipe.

From Structures to Lists

Recall recursive unions of structures from class.

Exercise 1 Write the template for a function which processes the following data and structure definition:
; A PosnList is one of:
;  - empty
;  - (cons (make-posn Number Number) PosnList)
; A Posn is (make-posn Number Number)
; Note: the posn structure is defined in the Beginning Student Language,
; and looks like:
; (define-struct posn (x y))

Exercise 2 Write 3 examples of PosnLists.

Exercise 3 Design a function many-positive? which consumes a PosnList and produces true if the x coordinates of all of the Posns in the PosnList are positive. If there aren’t any Posns in the PosnList, then they are all positive.

Worlds and Posns

Exercise 4 Design a function draw which consumes a PosnList and draws all the Posns on an empty scene. You can choose how to draw the Posns.

Exercise 5 Design a function move which consumes a PosnList and produces a PosnList where all of the y coordinates have increased by 1.

Exercise 6 Design a function add which consumes a PosnList and a KeyEvent and produces a PosnList with one more Posn added. The new Posn should have x and y coordinates determined by the function random.

Exercise 7 Using draw, move, and add, create an animation using big-bang. Be sure to specify what the data definition for World is.

Color Posns

Exercise 8 Design a data definition and structure definition for ColorPosn, which should have all the information that a Posn has, plus a color.

Exercise 9 Modify all your functions from the previous section to operate on ColorPosns. To start, you need to write the data definition for ColorPosnList analogous to PosnList.

Exercise 10 Using your modified functions, create a new animation using big-bang. You can choose how to color the new ColorPosns that are added. If you want to make the color random, then you can use the random function together with the color structure that comes with the 2htdp/image library.