Created
April 7, 2013 22:55
-
-
Save adamsmasher/5332949 to your computer and use it in GitHub Desktop.
Haskell Regex - Backtracking (...?)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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