Dear mrmyers, I cloned your repo however it's true that i complete have no idea about macro yet. It'd be nice for further reading as i'd work on this problem for long.
As soegaard suggests on IRC, to avoid the problem of creating new machines after every interaction, i'd try to make the struct #:mutable. Hope that'd be good. Thanks for great advice, Chi On 4 September 2015 at 12:00, Jens Axel Søgaard <jensa...@soegaard.net> wrote: > Hi Linh, > > There are many different representations of finite state machines. > > If you "just" need to simulate a single machine, a simple and efficient > approach is to represent each state as a Racket function (see example > below). > > #lang racket > (require racket/generator) > > ;; The turn-stile example from Wikipedia. > ;; > https://en.wikipedia.org/wiki/Finite-state_machine#Example:_coin-operated_turnstile > > ;; Current State Input Next State Output > ;; LOCKED coin UNLOCKED unlock turnstile so customer can > push through > ;; LOCKED push LOCKED none (custome can't move through) > ;; UNLOCKED coin UNLOCKED none > ;; UNLOCKED push LOCKED when customer is through, lock > turnstile > > (define inputs '(coin push push push coin coin push)) > (define get-input (sequence->generator inputs)) > > (define (LOCKED) > (match (get-input) > ['coin (displayln "turnstile is unlocked") > (UNLOCKED)] > ['push (displayln "you can't move through locked turnstile") > (LOCKED)] > [_ (DONE)])) > > (define (UNLOCKED) > (match (get-input) > ['coin (displayln "no need to pay when the turnstile is unlocked") > (UNLOCKED)] > ['push (displayln "customer goes through, turnstile is locked") > (LOCKED)] > [_ (DONE)])) > > (define (DONE) > (displayln "no more inputs")) > > (LOCKED) ; start turnstile in a locked state > > > > > 2015-09-03 16:29 GMT+02:00 Linh Chi Nguyen <nguyenlinhch...@gmail.com>: > >> Dear All, >> I'm a complete newbie in racket and need help in coding finite state >> machine/automata. Please pardon any of my ignorance. >> >> Thanks to this post of Tim Thornton, I see a very good way to code FSM: >> http://timthornton.net/blog/id/538fa6f2f09a16ba0674813d >> >> I'd summarise it as following: >> A finite state automaton has a number of states and each state has a name >> plus many transition rules. He structures it in racket as following: >> >> (struct automaton (current-state states)) >> (struct state (name actions)) >> (struct action (event result)) >> >> A simple automaton can be like this: >> (define simple-fsm (fsm 'A >> (list (fstate 'A (list (action 0 'A) >> (action 1 'B))) >> (fstate 'B (list (action 0 'B) >> (action 1 'A)))))) >> >> The automaton is in state A which has 2 transition rules: >> - if event 1 happens, the automaton jumps to state B, >> - if event 0 happens, it stays in state A. >> >> Then he writes some functions to update the automaton after each event >> (in the post). Plus this function he omits (I guess): >> (define fsm-update-state old-fsm new-state) >> (struct-copy automaton old-fsm [current-state new-state])) >> >> HERE IS THE QUESTION: >> >> Using this way, after each event, there'd be a NEW automaton created. So >> I'm worried about scaling. I'd like to generate 100 automata. In each >> cycle, I'd pair the automata to interact for 50 times. Which means that >> there'd be 100 new automata created for every single cycle. And I'd like to >> run at least 1000 cycles. >> >> Would there be any problem with scaling? If yes, is there a way around >> this? >> >> Any kind of comments and suggestions are welcomed and appreciated, >> Thank you really much, >> Chi >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Racket Users" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to racket-users+unsubscr...@googlegroups.com. >> For more options, visit https://groups.google.com/d/optout. >> > > > > -- > -- > Jens Axel Søgaard > > -- *Nguyen Linh Chi* -- You received this message because you are subscribed to the Google Groups "Racket Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to racket-users+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.