All pastes #1765869 Raw Edit

Inf III 12.2.a Rev 2

public text v1 · immutable
#1765869 ·published 2010-01-25 23:29 UTC
rendered paste body
;http://www.ccs.neu.edu/home/will/GC/Benchmarks/perm.sch

(define (permutations x)
  (let ((x x)
        (perms (list x)))
    (define (P n)
      (if (> n 1)
          (do ((j (- n 1) (- j 1)))
              ((zero? j)
               (P (- n 1)))
              (P (- n 1))
              (F n))))
    (define (F n)
      (set! x (revloop x n (list-tail x n)))
      (set! perms (cons x perms)))
    (define (revloop x n y)
      (if (zero? n)
          y
          (revloop (cdr x)
                   (- n 1)
                   (cons (car x) y))))
    (define (list-tail x n)
      (if (zero? n)
          x
          (list-tail (cdr x) (- n 1))))
    (P (length x))
    perms))
(permutations '(a b c))