News for hackers. Stuff that matters.
Re:Regular Expression Engine in 14 lines of Python by Peaker on Sunday November 06, 2011 @10:23AM (#3202851)
Transliteration into Haskell brings type-safety into it, and cuts it down to 7 lines:

  import Prelude hiding (seq)
  
  seq l r s = concatMap r (l s)
  alt l r s = l s ++ r s
  star e s = s : seq e (star e) s
  plus e = seq e (star e)
  char c (x:xs) 
    | x == c    = [xs]
    | otherwise = []
  char _ _ = []
  
  example = seq (char 'c') $ seq (plus (alt (char 'a') (char 'd'))) (char 'r')
  
  main = do
    line <- getLine
    mapM_ (putStrLn . ("Match with remainder: "++)) (example line)
(Note the original implementation failed to reuse itertools.chain, in Haskell I just use (++)).

There are no comments on this comment yet.