作者dryman (dryman)
看板PLT
標題[心得] clojure
時間Mon May 21 09:38:19 2012
上次fp聚會我會後有寫一點點clojure的demo
其中一個是算DFA,當時說要上傳不過忘記了orz
程式如下:
(def dfa
{:start :a,
:delta
{:a {0 :b,
1 :c}
:b {0 :a,
1 :b}
:c {0 :a,
1 :c}}
:accept #{:c :b}})
(defn process-dfa [dfa lst
]
(loop [state
(dfa
:start), lst lst
]
(if (seq lst
)
(recur (((dfa
:delta) state
) (first lst
))
(next lst
))
((dfa
:accept) state
))))
(process-dfa dfa
[0 1 0 0 1])
===> :b ; accepted
(process-dfa dfa
[0 1 0 0 1 0])
===> nil ; rejected
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.109.22.88
1F:推 godfat:感謝! 05/22 02:15