开发者

drscheme - finite state machine

thanks to people at this great site I managed to put together code that is nearly complete and working. I have one final question.

here is the code:

     (define (chartest ch)
       (lambda (x) (char=? x ch)))

     (define fsm-trans
        '((A (lambda (x) (string=? x "a") B), (B (lambda (x) (string=? x "a") C)))))

     (define (find-next-state state ch trl)
       (cond
         [(empty? trl) false] 
         [(and (symbol=? state (first (first trl)))
              ((second (first trl)) ch))
          (third (first trl))]
         [else (find-next-state state ch (rest trl))]))


     (define fsm-final '(C))

     (define start-state 'A)

     (define (run-fsm start trl final input)
       (cond
         [(empty? input)
          (cond
            [(member start final) true]
            [else false])]
         [else 
          (local ((define next (find-next-state start (first input) trl)))
            (cond
              [(boolean? next) false]
              [else (run-fsm next trl final (rest input))]))]))


     (run-fsm start-state fsm-trans fsm-final (string->list "ac"))

i have a problem with the transition function find-next-state. How can I define it in order to test the incoming characters and based on this either return true value when the fsm reaches final state or false value when it doesn't?

Thank you for your answer.

UPDATE:

Thank you for your answer and I am sorry that the code is confusing. I have repaired the definition of transtitions which now looks like this:

    (define fsm-trans
       '((A (lambda (x) (string=? x "a") B)
         (B (lambda (x) (string=? x "a") C)))))

But now I am trying to define the transition function. When I haven't had fixed transition character and I used char-alphabetic? and char-numeric?, these lines of code worked like a charm:

    (define (find-next-state state ch trl)
      (cond
        [(empty? trl) false] 
        [(and (symbol=? state (first (first trl)))
             ((second (first trl)) ch))
         (third (first trl))]
        [else (find-next-state state ch (rest trl))]))

But what should I change t开发者_C百科o work with the new definition of states in fsm-trans? When this code is entered in DrScheme, it shows up an error with line: ((second (first trl)) ch)).

Thank you for your further assistance!


It looks like the main problem in this code is a confusion over quotes, quasiquotes and unquotes. Specifically, '(foo (lambda (x) x) baz) is quoting the whole thing, so there is no function there, just a symbolic representation for one. Also, your use of , looks like you're confusing it as something that separates values in a list. Another problem is that the parens look mismatched. You probably want something like this instead, using a quasiquote:

(define fsm-trans
  `((A ,(lambda (x) (string=? x "a") B))
    (B ,(lambda (x) (string=? x "a") C))))

But given that you're unclear about these things, then it'll be much better to stick to simple quotes only, and use list when needed:

(define fsm-trans
  (list (list 'A (lambda (x) (string=? x "a") B))
        (list 'B (lambda (x) (string=? x "a") C))))

You probably have some more problems to get over, but doing that should get you in the right direction.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜