On this page:
Westerosi Politics
We Are Many. We Are Lambda.

Lab 6h Loops and Lambda

home work!

Purpose The purpose of this lab is to practice the use of existing list-processing functions and to explore the power of higher-order operations.

Westerosi Politics

; A House is a (make-house String Color)
; - where the first String represents the family name of this house
; - and the represents the color of this house's banner
(define-struct house (name color))
 
; Examples
(define ARRYN (make-house "Arryn" "blue"))
(define MORMONT (make-house "Mormont" "green"))
(define BARATHEON (make-house "Baratheon" "gold"))
(define STARK (make-house "Stark" "ghostwhite"))
(define GREYJOY (make-house "Greyjoy" "black"))
(define TARGARYEN (make-house "Targaryen" "black"))

Exercise 1 Design a function that takes a [List-of House] and produces an image of all their banners beside each other. Use foldr. To ease your artistic pain we have provided a function which draws a banner in a given color.

; draw-banner-in-color : Color -> Image
; Draws a banner in the given color
(define (draw-banner-in-color c)
  (above (rectangle 20 30 "solid" c)
         (flip-vertical (beside (right-triangle 10 30 "solid" c)
                                (flip-horizontal (right-triangle 10 30 "solid" c))))))

Exercise 2 The records in Westeros are a mess. The people are sick of spending all day looking for information about whose banner is approaching them. Design a function whose-banner which takes a [List-of House] and a Color and produces a list of all the houses whose banners are the given color. Use filter.

Exercise 3 The people of Westeros (and many other people) are confused about who is king. They want to develop a function for determining who has a claim to the throne. Design the function claim? which takes a House and a list of the names of houses with a claim to the throne and determines if the given House has a claim to the throne. Use ormap.

We Are Many. We Are Lambda.

Copy paste the following code to get started.

(require 2htdp/image)
(require 2htdp/universe)
 
(define WIDTH 500)
(define HEIGHT 500)
(define BG (empty-scene WIDTH HEIGHT))
 
; A World is a (make-world Natural [Listof Ball])
; - (world-t w) is the amount of time that has passed
; - (world-balls w) is the balls of the world
(define-struct world (t balls))
 
; A Ball is a (make-ball Natural Mode Color [Natural -> Posn])
; - (ball-r b) is b's radius
; - (ball-mode b) is b's mode
; - (ball-color b) is b's color
; - (ball-placement b) is a function, that given current time,
;   outputs a new coordinate to be drawn at
(define-struct ball (r mode color placement))
(define BALL-1 (make-ball 5 'solid 'red (λ (t) (make-posn 20 (modulo t HEIGHT)))))
 
; A Mode is one of
; - 'solid
; - 'outline
 
(define WORLD-1 (make-world 0 (list BALL-1)))

Exercise 4 Interpret BALL-1. How do you think it will behave?

Exercise 5 Design the function tick, that will take a World, and return one with the time incremented by one.

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

Exercise 7 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 8 Design the function draw that draws the 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? May it be a light to you in dark places, when all other lights go out.

(big-bang WORLD-1
         [on-tick tick]
         [to-draw draw])

Yay! We have a world! But it’s not particularly exciting. Let’s take the first step to making it much more awesome. What we want to do is add many different kinds of circles that move in mysterious ways. We’re going to add circles with a click, which will happen at a certain time and location of the mouse (is this similar to spawning different kinds of Tetra?).

; A BallGenerator is a [Natural Natural Natural -> [Natural -> Posn]]
; interpretation: 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 9 Design the BallGenerator move-vertically.

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

Exercise 11 Design the function place-ball, that given a World, Natural, Natural, and MouseEvent will output a World with a Ball added to it if the person clicked. Feel free to use any radius. color, or 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!

(big-bang (make-world 0 empty)
         [on-tick tick]
         [to-draw draw]
         [on-mouse place-ball])

Exercise 12 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 13 Update place-ball to select a random BallGenerator from GENERATORS.

Exercise 14 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 15 Update place-ball to randomly use a Mode.

Exercise 16 Update place-ball to randomly make a color. make-color should help (check out the docs).

Exercise 17 Go crazy. Create as many BallGenerators as you want to and add them to your GENERATORS. Have a Ball appear in a totally random place, make it zig-zag, the World is your oyster and WIDTH and HEIGHT are the limit!

Exercise 18 Go crazy again. Change your BallGenerators to make them move faster. (* 2 t) anybody?

Exercise 19 Tune in, turn off, drop out, drop in, switch on, switch off, and explode by changing "button-down" in your place-ball to "drag".