permutations 
Author Message
 permutations

Hello!

I have a prog that makes permutations of lists.

(permute '(a b c)) =>  ((a b c) (a c b) (b a c) (b c a) (c a b) (c b a))

I want a function so I can give it an argument so it gives len
permutations

(permute '(a b c d) 2) => ((a b) (a c) (a d) ... )

Thanks for any help
Bo

;;; The prog is here

(define flatten
  (lambda (ls)
    (apply append ls)))

(define remove
  (lambda (item ls)
    (filter (lambda (x) (not (eq? x item))) ls)))

(define permute
  (lambda (s)
    (if (null? s)
        '(())
        (flatten
         (map (lambda (x)
                (map (lambda (q) (cons x q))
                     (permute (remove x s))))
              s)))))

(define filter
  (lambda (pred ls)
    (cond ((null? ls) '())
          ((pred (car ls))
           (cons (car ls) (filter pred (cdr ls))))
          (else
           (filter pred (cdr ls))))))



Sat, 19 Apr 2003 02:08:25 GMT  
 permutations

Quote:

>I have a prog that makes permutations of lists.
>I want a function so I can give it an argument so it gives len
>permutations
>(permute '(a b c d) 2) => ((a b) (a c) (a d) ... )

Write a helper function that does *combinations* of len elements of
the list, and then use your existing permute function on the results.

Hint for combinations:  Any 2-element combination of (a b c d) either
contains A or doesn't contain A.



Sat, 19 Apr 2003 02:22:02 GMT  
 permutations

Quote:

>I want a function so I can give it an argument so it gives len
>permutations

Alternatively (duh) change the base case of your PERMUTE.
What should (permute '(a b c d) 0) return?


Sat, 19 Apr 2003 02:23:41 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. all-permutations is faster than permutation

2. A permutation on permutations

3. Generating permutations

4. Finding permutations

5. All Permutations

6. All permutations

7. Permutation

8. Permutations

9. Linked list to Permutation vector

10. Permutations

11. Puzzler: function of permutation vector

12. Fwd: re: Puzzler: Permutations and Anagrams

 

 
Powered by phpBB® Forum Software