4 ms·
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 rea
by alexgartrell 15y ago
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
- alexgartrell 15y agoOk, 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)