[racket] Finite State Machines of Arbitrary Size using Racket's composable control

From: Eli Barzilay (eli at barzilay.org)
Date: Thu May 24 13:18:21 EDT 2012

Just now, Galler wrote:
> Eli,
> 
> I fully agree with you that any FSM is equivalent to functional 
> composition and can be implemented in the manner you show.
> 
> However , in the way you've implemented the signal-handlers
> 
>   (define (B i)
> >       (if (= 0 (get i))
> >         (begin (printf "~s)" (sub1 i))
> >                (next A i))
> 
> I believe you have the signal handler B both reading the signal (get i) 
> and advancing to the next position in the signal-stream (next A i)

IIUC, you could do that with something like this:

  (define (encode v)
    (define (get i) (vector-ref v i))
    (define last (sub1 (vector-length v)))
    (define (next S i) (when (< i last) (S (add1 i))))
    (define (B signal i)
      (if (= 0 signal)
        (begin (printf "~s)" (sub1 i))
               A)
        B))
    (define (A signal i)
      (if (= 0 signal)
        A
        (begin (printf "(~s " i)
               B)))
    (for/fold ([state A]) ([signal (in-vector v)] [i (in-naturals)])
      (state signal i)))

But it seems redundant since the abstraction was practically there in
`next'.

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                    http://barzilay.org/                   Maze is Life!

Posted on the users mailing list.