Skip to content

Instantly share code, notes, and snippets.

@palladin
Created July 15, 2011 11:21
Show Gist options
  • Save palladin/1084511 to your computer and use it in GitHub Desktop.
Save palladin/1084511 to your computer and use it in GitHub Desktop.
Functional style Regex engine
let char c (s : string) = seq { if s.Length > 0 && s.[0] = c then yield s.Substring(1) }
let (=>) l r s = seq { for sl in l s do for sr in r sl -> sr }
let (<|>) l r s = seq { yield! l s; yield! r s }
let rec (<*>) e s = seq { yield s; yield! (e => (<*>) e) s }
let (<+>) e = e => (<*>) e
// example c(a|d)+r
let pattern = char 'c' => (<+>) (char 'a' <|> char 'd') => char 'r'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment