Created
March 18, 2011 07:51
-
-
Save cushon/875749 to your computer and use it in GitHub Desktop.
Subset and permutations without explicit recursion. The permutation implementation uses a fixed point combinator.
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 (subsets l) | |
(foldl (lambda (x y) | |
(foldr (lambda (u v) (cons (cons x u) (cons u v))) | |
'() y)) | |
'(()) l)) | |
(define permutations | |
(lambda (l) | |
(((lambda (f) | |
((lambda (x) (f (lambda arg (apply (x x) arg)))) | |
(lambda (x) (f (lambda arg (apply (x x) arg)))))) | |
(lambda (f) | |
(lambda (l acc) | |
(if (empty? l) (list acc) | |
(foldr (lambda (o w) | |
(foldr cons w | |
(f (filter (lambda (t) (not (= t o))) l) | |
(cons o acc)))) | |
'() | |
l))))) l '()))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment