Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7195885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:40:11+00:00 2026-05-28T20:40:11+00:00

I am trying to learn a bit of clojure by porting a toy NFA

  • 0

I am trying to learn a bit of clojure by porting a toy NFA regexp matcher. Obviously my main issue is representing and manipulating graphs. I hit a working solution, but my implementation (using gensym to emulate pointers, basically) leaves me with a yucky taste in my mouth.

Care to suggest improvements, both for representing graphs and for general readability and idioms? (the original imperative solution is much more readable than my current one is).

Cheers!

; This is a port of Matt Might's 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 #{}))
  ([nfa nfa_state_name string visited]
   (let [nfa_state (nfa_state_name (:states nfa))
         new_visited (conj visited nfa_state_name)]
     (cond
       (contains? visited nfa_state_name) false
       (empty? string) (or (:final nfa_state)
                           (some #(nfa-matches? nfa % string new_visited) 
                                 (:empty_transitions nfa_state)))
       (not (empty? string)) (or 
                               (some #(nfa-matches? nfa % (.substring string 1)) (get (:char_transitions nfa_state) (.charAt string 0) #{}))
                               (some #(nfa-matches? nfa % string new_visited) (:empty_transitions nfa_state)))
       :else false))))

(defn matches? [nfa string]
  "Matches a string against an NFA"
  (nfa-matches? nfa (:entry nfa) string))

(defn c [ch]
  "Creates an NFA which matches the character 'c'"
  (let [in (gensym)
        out (gensym)]
    (Nfa. in out {in (NfaState. false {ch #{out}} #{}) out (NfaState. true {} #{})})))

(defn e []
  "Creates an NFA which matches the empty string"
  (let [in (gensym)
        out (gensym)]
    (Nfa. in out {in (NfaState. false {} #{out}) out (NfaState. true {} #{})})))

(defn rep [nfa]
  "Creates an NFA which matches zero or more repetitions of the given NFA"
  (add-empty-edge (add-empty-edge nfa (:entry nfa) (:exit nfa)) (:exit nfa) (:entry nfa)))

(defn- update-final [nfastate new_final]
  (NfaState. new_final (:char_transitions nfastate) (:empty_transitions nfastate)))

(defn s [first second]
  "Creates an NFA which matches a sequence of two provided NFAs"
  (add-empty-edge (Nfa. (:entry first) (:exit second) (merge (update-in (:states first) [(:exit first)] update-final false) (update-in (:states second) [(:exit second)] update-final true))) (:exit first) (:entry second)))

(defn ou [first second]
  "Creates an NFA which matches either provided NFA"
  (let [in (gensym)
        out (gensym)]
    (add-empty-edge (add-empty-edge (Nfa. in out (assoc (merge (update-in (:states first) [(:exit first)] update-final false) (update-in (:states second) [(:exit second)] update-final false)) in (NfaState. false {} #{(:entry first) (:entry second)}) out (NfaState. true {} #{}))) (:exit first) out) (:exit second) out)))

; sugar
(defn st [string]
  "Creates an NFA which matches the provided string"
  (if (empty? string)
    (e)
    (s (c (.charAt string 0)) (st (.substring string 1)))))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T20:40:12+00:00Added an answer on May 28, 2026 at 8:40 pm

    Maps and sets are an excellent data such structures because they are visible durring development and can be easily manipulated using the zipper library which you may find useful for this type of problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to learn a bit of CSS and I want a horizontal navbar and
Trying to learn a bit about PDO and is going through this tutorial .
I am trying to learn a bit of Ruby. I've installed Ruby on my
I'm trying to learn a little bit C++ and Boost.Asio. I'm trying to compile
I'm trying to learn more about WPF and I've read a bit about Model-View-ViewModel
I was trying to learn unit test with PHP. I know it's a bit
In trying to learn JavaScript closures, I've confused myself a bit. From what I've
I'm trying to learn a bit about bash-commands -- more specifically about backup-scripts. Unfortunately,
I was trying to learn a bit about h264 by looking at the bitstream
I'm trying to learn a bit of LINQ but I'm having compile issues right

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.