@@ -55,6 +55,26 @@ init' s f = Step (\x -> let (s',out) = f x s
55count : Automaton a Int
55count : Automaton a Int
56count = init 0 (\_ c -> c + 1)
56count = init 0 (\_ c -> c + 1)
58+type Queue t = ([t],[t])
60+enqueue x (en,de) = (x:en, de)
63+ (en,[]) -> enqueue ([], reverse en)
64+ (en,hd::tl) -> Just (hd, (en,tl))
66+-- Computes the running average of the last `n` inputs.
67+average : Int -> Automaton Float Float
69+ let step n (ns,len,sum) =
70+ if len == k then stepFull n (ns,len,sum)
71+ else ((enqueue n ns, len+1, sum+n), (sum+n) / (len+1))
72+ stepFull n (ns,len,sum) =
74+ Nothing -> ((ns,len,sum), 0)
75+ Just (m,ns') -> let sum' = sum + n - m
76+ in ((enqueue n ns', len, sum'), sum' / len)
77+ in init' (empty,0,0) step
59{-- TODO(evancz): move this code to the Form library so people can find it.
79{-- TODO(evancz): move this code to the Form library so people can find it.