Skip to content

Instantly share code, notes, and snippets.

@ceving
Created March 21, 2017 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ceving/408df43e97a3f889e2debddafe6f0f32 to your computer and use it in GitHub Desktop.
Save ceving/408df43e97a3f889e2debddafe6f0f32 to your computer and use it in GitHub Desktop.
Permute regular patterns
(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