Skip to content

Instantly share code, notes, and snippets.

@adamsmasher
Created April 7, 2013 22:55
Show Gist options
  • Save adamsmasher/5332949 to your computer and use it in GitHub Desktop.
Save adamsmasher/5332949 to your computer and use it in GitHub Desktop.
Haskell Regex - Backtracking (...?)
import Control.Applicative
data Regex =
Literal Char
| Concat Regex Regex
| Altern Regex Regex
| Star Regex
match :: Regex -> String -> Bool
match r s = "" `elem` (consume r s)
consume :: Regex -> String -> [String]
consume (Literal c) [] = []
consume (Literal c) (c':cs) = if c == c' then [cs] else []
consume (Concat r1 r2) s = concat $ consume r2 <$> consume r1 s
consume (Altern r1 r2) s = (consume r1 s) ++ (consume r2 s)
consume (Star r) s = s : (concat $ consume (Star r) <$> consume r s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment