Created
March 21, 2017 16:04
-
-
Save ceving/408df43e97a3f889e2debddafe6f0f32 to your computer and use it in GitHub Desktop.
Permute regular patterns
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
(define (constant c) | |
(lambda (state) | |
(map (lambda (x) (cons c x)) state))) | |
(define (sequence first . rest) | |
(if (null? rest) | |
first | |
(lambda (state) | |
((apply sequence rest) (first state))))) | |
(define (alternation first second . rest) | |
(lambda (state) | |
(append (first state) | |
(if (null? rest) | |
(second state) | |
((apply alternation (cons second rest)) state))))) | |
(define-syntax perx | |
(syntax-rules ::: () | |
((_ . expr) | |
(letrec-syntax ((p (syntax-rules (or) | |
((_ (or a ...)) (alternation (p a) ...)) | |
((_ (x ...)) (sequence (p x) ...)) | |
((_ x) (constant x))))) | |
(map (lambda (l) (apply string-append l)) | |
(map reverse ((p expr) '(())))))))) | |
;; (perx (or ("a" (or "b" "c") "d") "e")) => ("abd" "acd" "e") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment