& let state = #{:position (0, 0), :heading 0, :pencolor :white} & let command = (:forward, 10) & match command with { & (:goto, (x, y)) -> assoc (state, :position, (x, y)) & & (:home) -> do state > & & assoc (_, :position, (0, 0)) > & & assoc (_, :heading, 0) & & (:clear) -> do state > & & assoc (state, :position, (0, 0)) > & & assoc (_, :heading, 0) & (:right, turns) -> update (state, :heading, add (_, turns)) & (:left, turns) -> update (state, :heading, sub (_, turns)) & (:forward, steps) -> { & let #{heading, position, ...} = state & let unit = heading/vector (heading) & let vect = mult (steps, unit) & update (state, :position, add (vect, _)) & } & (:back, steps) -> { & let #{heading, position, ...} = state & let unit = heading/vector (heading) & let vect = mult (steps, unit) & update (state, :position, sub (_, vect)) & } & & (:penup) -> assoc (state, :pendown?, false) & & (:pendown) -> assoc (state, :pendown?, true) & & (:penwidth, pixels) -> assoc (state, :penwidth, pixels) & & (:pencolor, color) -> assoc (state, :pencolor, color) & & (:setheading, heading) -> assoc (state, :heading, heading) & & (:loadstate, position, heading, visible?, pendown?, penwidth, pencolor) -> #{position, heading, visible?, pendown?, penwidth, pencolor} & & (:show) -> assoc (state, :visible?, true) & & (:hide) -> assoc (state, :visible?, false) & & (:background, _) -> state & } box foos = [] repeat 4 { update! (foos, append (_, :foo)) } do foos > unbox > count