#lang racket
(require racket/generator)
(define (permute v)
(generator ()
(define count 0)
(define len (vector-length v))
(define sorted (sort (vector->list v) <))
(let loop ([level 0])
(if (> level (sub1 len))
(begin
(set! count (add1 count))
(yield v))
(for ([i sorted])
(unless (for/or ([priorLevel (in-range 0 level)])
(= i (vector-ref v priorLevel)))
(vector-set! v level i)
(loop (add1 level))))))))
(define (permutations v)
(define r '())
(define p (permute v))
(let loop ()
(let ([x (p)])
(unless (void? x)
(set! r (cons (vector->list x) r))
(loop))))
r)
(displayln (permutations (vector 1 2 3)))
(require racket/generator)
(define (permute v)
(generator ()
(define count 0)
(define len (vector-length v))
(define sorted (sort (vector->list v) <))
(let loop ([level 0])
(if (> level (sub1 len))
(begin
(set! count (add1 count))
(yield v))
(for ([i sorted])
(unless (for/or ([priorLevel (in-range 0 level)])
(= i (vector-ref v priorLevel)))
(vector-set! v level i)
(loop (add1 level))))))))
(define (permutations v)
(define r '())
(define p (permute v))
(let loop ()
(let ([x (p)])
(unless (void? x)
(set! r (cons (vector->list x) r))
(loop))))
r)
(displayln (permutations (vector 1 2 3)))
((3 2 1) (3 1 2) (2 3 1) (2 1 3) (1 3 2) (1 2 3))
'My Computer Programs' 카테고리의 다른 글
(Windows) show-ip.bat (0) | 2012.10.18 |
---|---|
(Racket) processor-count (0) | 2012.10.18 |
(Racket) tLISP (0) | 2012.09.25 |
(Racket) parse-list (0) | 2012.09.24 |
(newLISP) get web page, KOSPI200 시세 얻기 (0) | 2012.09.23 |