【发布时间】:2011-06-09 01:05:36
【问题描述】:
感谢这个伟大网站的人们,我设法将几乎完整且有效的代码组合在一起。我还有最后一个问题。
代码如下:
(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))。
感谢您的进一步帮助!
【问题讨论】:
-
您已经更新了问题,但仍然被破坏了——如果您想使用引号,请复制我使用的第一个示例原样 - 那里是 反引号 和 逗号。但就像我说的,在这个阶段你使用第二个例子好多了。
-
@Eli Barzilay:当我使用第二个示例时,它给了我一个关于 B 的错误。它说它只需要 lambda 中的一个表达式,但在 B 中发现了一个额外的部分。现在我是不确定我应该使用哪个定义。谢谢您的回答。我让它工作的唯一方法是这样的: (define fsm-trans (list (list 'A (lambda (x) ((string=? x "a") 'B))) (list 'B (lambda (x ) ((string=? x "a") 'C))))))
-
您可能正在使用学生语言。如果您的课程需要这样做,那么您需要向您的讲师咨询如何编写代码。如果没有,那么您应该切换到普通的球拍语言(使用
#lang racket,并将语言设置为“检测源语言”设置。)