上次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