Note that there are some explanatory texts on larger screens.

plurals
  1. POdrscheme - finite state machine
    text
    copied!<p>thanks to people at this great site I managed to put together code that is nearly complete and working. I have one final question.</p> <p>here is the code:</p> <pre><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-&gt;list "ac")) </code></pre> <p>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?</p> <p>Thank you for your answer.</p> <p>UPDATE:</p> <p>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:</p> <pre><code> (define fsm-trans '((A (lambda (x) (string=? x "a") B) (B (lambda (x) (string=? x "a") C))))) </code></pre> <p>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:</p> <pre><code> (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))])) </code></pre> <p>But what should I change to 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)).</p> <p>Thank you for your further assistance!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload