drscheme - 有限状态机
感谢这个伟大网站上的人们,我设法将几乎完整且有效的代码组合在一起。我还有最后一个问题。
这是代码:
(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"))
我对转换函数 find-next-state 有问题。我如何定义它以测试传入的字符,并基于此在 fsm 达到最终状态时返回 true 值,或者在未达到最终状态时返回 false 值?
谢谢您的回答。
更新:
感谢您的回答,很抱歉代码令人困惑。 我已经修复了转换的定义,现在看起来像这样:
(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))]))
但是我应该改变什么才能使用 fsm-trans 中状态的新定义? 当在 DrScheme 中输入此代码时,它会显示错误行:((second (first trl)) ch))。
感谢您的进一步帮助!
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 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)).
Thank you for your further assistance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来这段代码中的主要问题是对引号、准引号和非引号的混淆。具体来说,
'(foo (lambda (x) x) baz)
引用了整个事物,因此那里没有函数,只是一个函数的符号表示。另外,您对,
的使用看起来像是您将其与分隔列表中的值的东西混淆了。另一个问题是括号看起来不匹配。您可能想要这样的东西,使用准引号:但是鉴于您不清楚这些事情,那么最好只使用简单的引号得多 ,并在需要时使用
list
:您可能还有一些问题需要克服,但这样做应该会让您朝着正确的方向前进。
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: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:You probably have some more problems to get over, but doing that should get you in the right direction.