4 (append (cdr ls) (list (car ls)))))
6 (define (permutations ls)
7 (let ((len (length ls)))
10 (let r ((result '()) (ls1 ls) (rotate-times len))
11 (if (= 0 rotate-times)
14 (map (lambda (x) (cons (car ls1) x))
15 (permutations (cdr ls1))))
16 (rotate ls1) (- rotate-times 1)))))))
18 (define (permutation2 ls) ;generator style
19 (let ((len (length ls)))
22 (let r ((result '()) (ls1 ls) (rotate-times len))
23 (if (= 0 rotate-times)
26 (map (lambda (x) (cons (car ls1) x))
27 (permutations (cdr ls1))))
28 (rotate ls1) (- rotate-times 1)))))))
31 (lambda (x) (k (cons v x))))
34 (define (reverse-by-first-elem ls)
36 (append (reverse (list-head ls n))
39 (define (max-flips ls)
40 (let r ((times 0) (nls ls))
44 (r (+ 1 times) (reverse-by-first-elem nls))))))
46 ;; (define q '(1 2 3 4 5 6 7 8 9 10 11 12))
48 ;; (display (apply max (map max-flips (permutations ))))
52 ;; (max-flips '(7 6 5 4 3 2 1))
55 (set! pile (cons (max-flips p) pile)))
57 (define (incred-permutation proc cur rest)
59 (proc (max-flips cur))
61 (lambda (x) (incred-permutation
64 (remove (lambda (y) (= x y)) rest)))
67 (define (incred-permutation2 proc cur rest)
69 (proc (max-flips cur))
71 (lambda (M R) (incred-permutation2
77 (define (for-each2 P L)
81 (P mid (append (reverse left) right))
82 (if (not (null? right))
87 (define (for-each3 P L)
90 ;; (for-each2 (lambda (M R)
91 ;; (format #t "middle is: ~a\n" M)
92 ;; (format #t "rest is: ~a\n" R))
95 ;; (use-modules (statprof))
97 ;; (statprof-reset 0 50000 #t)
101 (f (lambda (x) (if (> x max)
103 ;; (use-modules (srfi srfi-1))
104 ;; (incred-permutation f '() '(1 2 3 4 5 6 7 8))
105 ;; (incred-permutation2 f '() '(1 2 3 4 5 6 7 8 9 10 11 12))
106 (incred-permutation2 f '() '(1 2 3 4 5 6 7 8 9 10 11 12))
110 ;; (statprof-display)
112 ;; (define q (make-bytevector 7))