News for hackers. Stuff that matters.
Re:A regular expression matcher in 19 lines of Clojure by alexgartrell on Sunday October 16, 2011 @03:40PM (#3118093)
I'm no haskell hacker (by a long, long, long shot[0]), and I've played a lot more with Clojure than with Haskell, but I still found this easier to write and read in haskell than in clojure.

    matches :: String -> String -> Bool
    matches rs = dm ((fixHead . fixTail) rs) where
      fixHead ('^':rs) = rs
      fixHead rs = '.':'*':rs
      fixTail rs = case (reverse rs) of
        ('$':rrs) -> reverse rrs
        rrs -> reverse ('*':'.':rrs)
      dm "" "" = True
      dm ('.':'*':rs) (c:cs) = (dm ('.':'*':rs) cs) || (dm rs (c:cs))
      dm (r:'*':rs) (c:cs) = (dm rs (c:cs)) || (c == r && (dm (r:'*':rs) cs))
      dm ('.':rs) (c:cs) = dm rs cs
      dm (r:rs) (c:cs) = r == c && dm rs cs
      dm (_:'*':[]) "" = True
      dm _ _ = False
[0] I'm particularly ashamed of fixHead and fixTail and the way I special cased matching a star and nothing else against the empty string

edit: couldn't resist doing the golf thing

    matches :: String -> String -> Bool
    matches rs = dm (fixHead rs) where
      fixHead ('^':rs) = rs
      fixHead rs = '.':'*':rs
      cm r c = r == '.' || r == c
      dm (r:'*':rs) (c:cs) = (dm rs (c:cs)) || ((cm r c) && (dm (r:'*':rs) cs))
      dm (_:'*':"") "" = True
      dm (r:rs) (c:cs) = cm r c && dm rs cs
      dm "" _ = True
      dm "$" "" = True
      dm _ _ = False
Re:A regular expression matcher in 19 lines of Clojure by alexgartrell on Sunday October 16, 2011 @05:16PM (#3118324)
Ok, now I'm done

    matches :: String -> String -> Bool
    matches ('^':rs) = dm rs where
      cm r c = r == '.' || r == c
      dm (r:'*':rs) (c:cs) = (dm rs (c:cs)) || ((cm r c) && (dm (r:'*':rs) cs))
      dm (_:'*':"") "" = True
      dm (r:rs) (c:cs) = cm r c && dm rs cs
      dm "" _ = True
      dm "$" "" = True
      dm _ _ = False
    matches rs = matches ("^.*" ++ rs)