Unexpected behavior permutations vs in-permutations

When I use permutations or in-permutations I get different orderings for the permutations returned, is there some good reason for that?
From the documentation I would expect identical behaviour, it states:

(in-permutations lst)
[...] It is equivalent to (in-list (permutations l))

But that is not true, I get:

> (permutations '(1 2 3))
'((1 2 3) (2 1 3) (1 3 2) (3 1 2) (2 3 1) (3 2 1))
> (sequence->list (in-permutations '(1 2 3)))
'((3 2 1) (2 3 1) (3 1 2) (1 3 2) (2 1 3) (1 2 3))
> (reverse (sequence->list (in-permutations '(1 2 3))))
'((1 2 3) (2 1 3) (1 3 2) (3 1 2) (2 3 1) (3 2 1))

I am using a Racket v8.6.0.4 [cs] snapshot on linux, I get the same behavior with Racket v8.5 [cs].

1 Like

I’m not sure why there’s a reverse at https://github.com/racket/racket/blob/master/racket/collects/racket/list.rkt#L773. Remove that and you get the expected ordering.

1 Like

I think it’s a copy-and-paste leftover from the permutations variant. In permutations, we need to reverse the list since we are building the list backward (by cons’ing from back to front). But in in-permutations, we produce the value from front to back, so we shouldn’t need that reversing.

2 Likes