Implementation of the Johnson Trotter algorithm.
JohnsonTrotter.first [ "a", "b", "c" ]
|> JohnsonTrotter.getState
|> Maybe.map JohnsonTrotter.next
|> Maybe.andThen JohnsonTrotter.getPermutation
-- [ "a", "c", "b" ]
Represents the next permutation, if there is one.
first : List a -> Step a
Initialises permutations. The first argument specifies the list that you want to get permutations for.
next : State a -> Step a
Get the next permutation. The first argument is the current State
of the algorithm, which is in the Step
returned by first
and next
. Use the getState
helper funtion to retrieve the State
from Step
.
getState : Step a -> Maybe (State a)
Get the State
from Step
, if there is one.
getPermutation : Step a -> Maybe (List a)
Get the permutation from Step
, if there is one.