感謝這個偉大網站上的人們,我設法將幾乎完整並且可行的代碼放在一起。我有一個最後的問題。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"))
我有帶轉移功能找到-下一狀態的問題。我怎樣才能定義它來測試傳入的字符,並基於這個,當fsm達到最終狀態時返回真值,否則返回假值。
謝謝你的回答。
UPDATE:
謝謝您的回答,我很抱歉,該代碼是混亂的。 我修好transtitions的定義,現在看起來是這樣的:
(define fsm-trans
'((A (lambda (x) (string=? x "a") B)
(B (lambda (x) (string=? x "a") C)))))
但現在我試圖定義轉換函數。當我沒有固定的過渡字符,我用字符字符?和char-numeric?,這些代碼行像一個魅力工作:
(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))。
感謝您的進一步協助!
您已經以*仍然*破壞的方式更新了問題 - 如果您想使用引號,請按照原樣複製我使用的第一個示例* - 那些引號*和*逗號* 。但就像我說的那樣,在這個階段使用第二個例子**會更好**。 – 2011-06-08 23:15:20
@Eli Barzilay:當我使用第二個例子時,它給了我一個有關B的錯誤。它表示它期望lambda中只有一個表達式,但在其中找到一個額外的部分:B.現在我不確定我應該使用哪個定義。謝謝您的回答。 (定義fsm-trans (list(list'A(lambda(x)((string =?x「a」)'B))) (list'B(lambda (x)((string =?x「a」)'C))))) – Vojtech 2011-06-09 09:07:14
您可能正在使用學生語言。如果您的課程需要這樣做,那麼您需要向您的教師諮詢如何編寫代碼的建議。如果不是,那麼你應該切換到普通的球拍語言(使用'#lang racket',並將語言設置爲「源語言檢測」設置)。 – 2011-06-09 11:11:00