[plt-scheme] Shriram Krishnamurthi's "automaton" macro from "Swine before Perl"

From: Chris Wright (wrightca at hotmail.com)
Date: Mon Nov 8 05:50:22 EST 2004

Hi.

I think I understand Shriram's macro:

(define-syntax automaton
  (syntax-rules (-> :)
    [(_ init-state
        (state : (cndn -> new-state) ...) ...)
     (letrec ([state (lambda (stream)
                       (or (empty? stream)
                           (case (first stream)
                             [(cndn)
                              (new-state (rest stream))]
                             ...
                             [else false])))]
              ...)
       init-state)]))

(define a
  (automaton init
             (init : (c -> loop))
             (loop : (a -> loop)
                     (d -> loop)
                     (r -> end))
             (end : )))

In use:

Welcome to DrScheme, version 208.
Language: Pretty Big (includes MrEd and Advanced).
>(a '(c a r))
#t
>(a '(c d a r))
#t
>(a '(c x))
#f

but:
>(a '(c a d a))
#t


As an exercise, I thought I'd try seeing if I could make it check that when 
the stream is empty, new-state is null, and I'm having trouble with my 
ellipses!

(define-syntax automaton
  (syntax-rules (-> :)
    [(_ init-state
        (state : (cndn -> new-state) ...) ...)
     (letrec ([state (lambda (stream)
                       (or (and (empty? stream)
                                (eq? new-state #f))
                           (case (first stream)
                             [(cndn)
                              (new-state (rest stream))]
                             ...
                             [else false])))]
              ...)
       init-state)]))

(define a
  (automaton init
             (init : (c -> loop))
             (loop : (a -> loop)
                     (d -> loop)
                     (r -> end))
             (end : #f)))

This gives:

syntax: too few ellipses for pattern variable in template in: new-state

(And I'm not even sure that the test (eq? new-state #f) will do what I want 
it too, but that's a separate issue)


Could someone help me understand this?

Thanks very much.

Chris Wright

_________________________________________________________________
SEEK: Now with over 50,000 dream jobs! Click here:   
http://ninemsn.seek.com.au?hotmail



Posted on the users mailing list.