I am trying to learn a little clojure by placing an NFA regexp matcher toy . Obviously, my main problem is representing and manipulating graphs. I ended up in a working solution, but my implementation (using gensym
to emulate pointers, mostly) leaves me with a bright taste in my mouth.
Consider in order to propose improvements, both for presentation of graphs, and for general readability and idioms? (the original imperative decision is much more readable than my current one).
Hurrah!
; This is a port of Matt Might toy regexp library described in ; http://matt.might.net/articles/implementation-of-nfas-and-regular-expressions-in-java/ ; char_transitions is a map that associates a character with a set ; of target state names ; empty_transitions is a set of target state names (defrecord NfaState [final char_transitions empty_transitions]) ; 'entry' and 'exit' are state names ; 'states' is a map that associates state names with states (defrecord Nfa [entry exit states]) (defn- add-empty-edge [nfa curr_state_name next_state_name] (let [curr_state (curr_state_name (:states nfa))] (Nfa. (:entry nfa) (:exit nfa) (assoc (:states nfa) curr_state_name (NfaState. (:final curr_state) (:char_transitions curr_state) (conj (:empty_transitions curr_state) next_state_name)))))) (defn- nfa-matches? ([nfa nfa_state_name string] (nfa-matches? nfa nfa_state_name string
source share