47 lines
1.7 KiB
Plaintext
47 lines
1.7 KiB
Plaintext
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) -> {
|
|
& print! ("matched forward")
|
|
& let #{heading, position, ...} = state
|
|
& print! ("extracted {heading} and {position} from state")
|
|
& let unit = heading/vector (heading)
|
|
& print! ("unit vector at {heading}: {unit}")
|
|
& let vect = mult (steps, unit)
|
|
& print! ("update vector: {vect}")
|
|
& let new_state = update (state, :position, add (vect, _))
|
|
& print! ("new state: {new_state}")
|
|
& new_state
|
|
& }
|
|
& & (: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
|
|
& }
|
|
let #{heading, position, ...x} = state
|
|
let unit = heading/vector (heading)
|
|
unit
|
|
|