@@ -40,20 +40,26 @@ pure : (a -> b) -> Automaton a b
4040pure f = Step (\x -> (pure f, f x))
4141
4242-- Create an automaton with state. Requires an initial state and a step
43−-- function to step the state forward.
44−init : b -> (a -> b -> b) -> Automaton a b
45−init s f = Step (\x -> let s' = f x s
46− in (init s' f, s'))
43+-- function to step the state forward. For example, an automaton that counted
44+-- how many steps it has taken would look like this:
45+--
46+-- count = state 0 (\\_ c -> c+1)
47+--
48+-- It is a stateful automaton. The initial state is zero, and the step function
49+-- increments the state on every step.
50+state : b -> (a -> b -> b) -> Automaton a b
51+state s f = Step (\x -> let s' = f x s
52+ in (state s' f, s'))
4753
4854-- Create an automaton with hidden state. Requires an initial state and a
4955-- step function to step the state forward and produce an output.
50−init' : s -> (a -> s -> (s,b)) -> Automaton a b
51−init' s f = Step (\x -> let (s',out) = f x s
52− in (init' s' f, out))
56+hiddenState : s -> (a -> s -> (s,b)) -> Automaton a b
57+hiddenState s f = Step (\x -> let (s',out) = f x s
58+ in (hiddenState s' f, out))
5359
5460-- Count the number of steps taken.
5561count : Automaton a Int
56−count = init 0 (\_ c -> c + 1)
62+count = state 0 (\_ c -> c + 1)
5763
5864type Queue t = ([t],[t])
5965empty = ([],[])
@@ -64,7 +70,7 @@ dequeue q = case q of
6470 (en,hd::tl) -> Just (hd, (en,tl))
6571
6672-- Computes the running average of the last `n` inputs.
67−average : Int -> Automaton (Number a) (Number a)
73+average : Int -> Automaton Float Float
6874average k =
6975 let step n (ns,len,sum) =
7076 if len == k then stepFull n (ns,len,sum)
@@ -74,30 +80,8 @@ average k =
7480 Nothing -> ((ns,len,sum), 0)
7581 Just (m,ns') -> let sum' = sum + n - m
7682 in ((enqueue n ns', len, sum'), sum' / len)
77− in init' (empty,0,0) step
83+ in hiddenState (empty,0,0) step
7884
79−{-- TODO(evancz): move this code to the Form library so people can find it.
80−
81−data DragState = Listen | Ignore | DragFrom (Int,Int)
82−
83−vecSub (x1,y1) (x2,y2) = (x1-x2,y1-y2)
84−
85−stepDrag (press,pos) (ds,form) =
86− let wrap ds' = (form, (ds',form)) in
87− case ds of
88− Listen -> wrap (if | not press -> Listen
89− | pos `isWithin` form -> DragFrom pos
90− | otherwise -> Ignore)
91− Ignore -> wrap (if press then Ignore else Listen)
92− DragFrom p0 ->
93− if press then (uncurry move (vecSub pos p0) form, (DragFrom p0, form))
94− else (let form' = uncurry move (vecSub pos p0) form in
95− (form', (Listen,form')))
96−
97−-- Create a draggable form that can be dynamically created and added to a scene.
98−draggable : Form -> Automaton (Bool,(Int,Int)) Form
99−draggable form = init' (Listen,form) stepDrag
100−--}
10185
10286{-- TODO(evancz): See the following papers for ideas on how to make this
10387library faster and better: