# Unexpected behavior permutations vs in-permutations

**URL:** <https://racket.discourse.group/t/unexpected-behavior-permutations-vs-in-permutations/1253>\
**Category:** Questions & Answers\
**Created:** [August 24, 2022, 12:38am UTC](https://racket.discourse.group/t/unexpected-behavior-permutations-vs-in-permutations/1253 "2022-08-24T00:38:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![simonls](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/simonls/32/170_2.png) [@simonls](https://racket.discourse.group/u/simonls)\
**Post date:** [August 24, 2022, 12:38am UTC](https://racket.discourse.group/t/unexpected-behavior-permutations-vs-in-permutations/1253/1 "2022-08-24T00:38:37Z")

</div>

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](https://docs.racket-lang.org/reference/pairs.html#%28def._%28%28lib._racket%2Flist..rkt%29._in-permutations%29%29) lst)  
> [...] It is equivalent to ([in-list](https://docs.racket-lang.org/reference/sequences.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._in-list%29%29) ([permutations](https://docs.racket-lang.org/reference/pairs.html#%28def._%28%28lib._racket%2Flist..rkt%29._permutations%29%29) l))

But that is not true, I get:

```scheme
> (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].

---

<div class="post-metadata">

**Author:** ![sorawee](https://avatars.discourse-cdn.com/v4/letter/s/ea5d25/32.png) [@sorawee](https://racket.discourse.group/u/sorawee)\
**Post date:** [August 24, 2022, 1:23am UTC](https://racket.discourse.group/t/unexpected-behavior-permutations-vs-in-permutations/1253/2 "2022-08-24T01:23:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sorawee](https://avatars.discourse-cdn.com/v4/letter/s/ea5d25/32.png) [@sorawee](https://racket.discourse.group/u/sorawee)\
**Post date:** [August 24, 2022, 1:28am UTC](https://racket.discourse.group/t/unexpected-behavior-permutations-vs-in-permutations/1253/3 "2022-08-24T01:28:05Z")

</div>

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.
