Skip to content

Instantly share code, notes, and snippets.

@cushon
Created March 18, 2011 07:51
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 cushon/875749 to your computer and use it in GitHub Desktop.
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.
(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