On this page:
We Are Many. We Are Lambda.
6.6

Lab 11 The Lambda Lab

lab!

Purpose: To practice the use of lambda in a fun and aesthetically pleasing way.

We Are Many. We Are Lambda.

Goals: Practice using functions as data. Practice designing functions that produce functions. Practice using lambda to create anonymous functions. Practice with world programs.

We will design a game in which you can create many balls which move across the screen in various ways. The player can add new balls by clicking on the screen.

Starter Code: Below is the data definition for a Ball.

; A Ball is a (make-ball Nat Mode Color [Nat -> Posn])
(define-struct ball [r mode color placement])
; INTERPRETATION:
; - r is the ball's radius
; - mode is the ball's mode
; - color is the ball's color
; - placement is a function that, given the current time,
;   outputs a new coordinate for the ball to be drawn at
 
; A Mode is one of:
; - "solid"
; - "outline"
 
(define BALL-1 (make-ball 3 "solid" "red" (λ (x) (make-posn 5 x))))
(define BALL-2 (make-ball 3 "solid" "blue" (λ (x) (make-posn (+ x 2) 3))))

Starter Code: Below is the data definition for a BallWorld, as well as the main function and some graphical constants.

; A BallWorld is a (make-world Nat [List-of Ball])
(define-struct ball-world [t balls])
; - where t is the amount of time that has passed
; - and balls is the balls of the world
 
(define WORLD-1 (make-ball-world 0 '()))
(define WORLD-2 (make-ball-world 10 (list BALL-1 BALL-2)))
 
 
 
; main : [List-of Ball] -> World
; Run the game with this list of initial balls
(define (main init-list)
  (big-bang (make-ball-world 0 init-list)
            [on-tick (λ (bw) (make-ball-world (add1 (ball-world-t bw))
                                              (ball-world-balls bw)))]
            [to-draw draw]
            [on-mouse place-ball]))
 
(define WIDTH 500)
(define HEIGHT 500)
(define BG (empty-scene WIDTH HEIGHT))

Exercise 1 Design the function draw-ball that given a Ball, a Posn, and an image, draws the ball at that point on that image.

Exercise 2 Design the function make-drawer that given a time will create a function that takes a Ball and an image and will draw it. Hint: Use draw-ball.

Exercise 3 Design the function draw that draws the ball world. Look at the helpers you’ve just written: especially the signature for make-drawer. What does it remind you of? How can you use that to help you?

We want to be able to add new balls when the user clicks the screen. Here are some data definitions that will help us:
; A BallGenerator is a [Nat Nat Nat -> [Nat -> Posn]]
; Given the time, x-coordinate, and y-coordinate of when and where a
; ball is created, create a function that, given the current time of
; the world, will output a Posn
 
; Example:
; move-horizontally : BallGenerator
(define (move-horizontally t0 x0 y0)
  (λ (t) (make-posn (modulo (+ x0 (- t t0)) WIDTH) y0)))
(check-expect ((move-horizontally 3 5 8) 10) ; 7 seconds have passed
              (make-posn 12 8))

Exercise 4 Design the BallGenerator move-vertically.

Exercise 5 Create a constant GENERATORS, which is a [List-of BallGenerator] that will keep track of all the BallGenerators you’ve made thusfar.

Exercise 6 Design the function place-ball, that given a BallWorld, x- and y- coordinate, and MouseEvent will output a BallWorld with a Ball added to it if the person clicked (this is represented by the MouseEvent "button-down"). Feel free to use any radius, color, and mode you like.

As far as the ball’s placement: look at the data definition of a Ball, the signature of this function, and the data definition of a BallGenerator. Feel free to use any BallGenerator you like, as long as it’s used correctly!

Exercise 7 Now comes the fun stuff. Design a function select-random, that given any non-empty list will select a random element from the list.

Exercise 8 Update place-ball to select a random BallGenerator from the GENERATORS you previously defined.

Exercise 9 Update place-ball to make a radius of random size. Make sure you don’t make radiuses of size 0, though, those aren’t very fun!

Exercise 10 Update place-ball to use a random make-color.

Exercise 11 Update place-ball by changing "button-down" to "drag".