Lecture notes: Quiz 1, HW 4 discussion

Quiz 1

We had our first in-class quiz!

Homework 4 discussion

We discussed the homework assignment. In particular, we went into some detail about how Pyret’s reactors work.

Pyret provides reactors so that users can create systems that change over time. Here’s an example of a simple animation built on reactors:

include image
include reactors

WIDTH = 160
HEIGHT = 90

fun tick(state :: Number) -> Number:
  if ((state + 10) >= WIDTH) or ((state + 10) >= HEIGHT):
    0
  else:
    state + 1
  end
end

DOT = circle(5, "solid", "red")

BACKGROUND = rectangle(WIDTH, HEIGHT, "solid", "white")

fun draw-dot(state :: Number) -> Image:
  overlay-xy(DOT, -1 * state, -1 * state, BACKGROUND)
end


animation = reactor:
  init: 0,
  to-draw: draw-dot,
  on-tick: tick,
end

When we call interact(animation), Pyret uses the reactor configuration to determine what to display on the screen. Pyret tracks the state of the system, which starts out as 0. Pyret renders this state to the screen using the draw-dot function. Every 28th of a second (the default tick interval) Pyret calls tick with the current state and stores the result as the new state of the system.