Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 0.75 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. module Regex (match) where
  2.  
  3. import Data.List
  4.  
  5. data Token = C Char | Start | End deriving (Eq)
  6.  
  7. match :: String -> String -> Bool
  8. match re = not . null . filter (not.null) . fmap match' . tails . tokenize
  9.     where match'   = flip (foldl' (>>=)) (compile re) . return
  10.           tokenize = (Start:) . (++[End]) . map C
  11.  
  12. compile = reverse . foldl' go []
  13.     where go acc c = case c of
  14.             '*' -> star (head acc) : tail acc
  15.             '.' -> single (const True) : acc
  16.             '$' -> single (End==) : acc
  17.             '^' -> single (Start==) : acc
  18.             c   -> single ((C c)==) : acc
  19.           single _ []     = []
  20.           single t (x:xs) = if t x then [xs] else []
  21.           star _ []       = [[]]
  22.           star m xs       = xs : (m xs >>= star m)