ceddlyburge / johnson-trotter / JohnsonTrotter

Implementation of the Johnson Trotter algorithm.

JohnsonTrotter.first [ "a", "b", "c" ]
    |> JohnsonTrotter.getState
    |> Maybe.map JohnsonTrotter.next
    |> Maybe.andThen JohnsonTrotter.getPermutation
-- [ "a", "c", "b" ]

Step Type


type Step a
    = Done
    | Next (State a) (List a)

Represents the next permutation, if there is one.

Initiating permutations

first : List a -> Step a

Initialises permutations. The first argument specifies the list that you want to get permutations for.

Iterating permutations

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.