Skip to content

Commit

Permalink
list: make permutations and in-permutations ordering agree
Browse files Browse the repository at this point in the history
In `permutations`, we need to reverse the list, because we want to build
the list backward by consing the last element until the first element.
`in-permutations`, however, generates each element from front-to-back,
so we should not need reversing.

Also adjust existing tests so that it checks that both orders agree.
  • Loading branch information
sorawee authored and mflatt committed Oct 2, 2022
1 parent 3180658 commit 89a67c5
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
6 changes: 3 additions & 3 deletions pkgs/racket-test-core/tests/racket/list.rktl
Original file line number Diff line number Diff line change
Expand Up @@ -490,10 +490,10 @@
(and (= (car l1) (car l2))
(loop (cdr l1) (cdr l2)))))))
(define (sorted-perms l)
(define l1 (sort (permutations l) perm<?))
(define l2 (sort (for/list ([p (in-permutations l)]) p) perm<?))
(define l1 (permutations l))
(define l2 (for/list ([p (in-permutations l)]) p))
(test #t equal? l1 l2)
l1)
(sort l1 perm<?))
(test '(()) sorted-perms '())
(test '((1)) sorted-perms '(1))
(test '((1 2) (2 1)) sorted-perms '(1 2))
Expand Down
2 changes: 1 addition & 1 deletion racket/collects/racket/list.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@
(when (> N 254) (error 'permutations "input list too long: ~e" l))
(define c (make-bytes (add1 N) 0))
(define i 0)
(define cur (reverse l))
(define cur l)
(define (next)
(define r cur)
(define ci (bytes-ref c i))
Expand Down

0 comments on commit 89a67c5

Please sign in to comment.