Derangements
Generate List.permutations
and keep the ones where no elements are in their original position. This is done by zipping each permutation with the original list, and keeping the ones where none of the zipped pairs are equal.
I am using the Z
infix zip operator with the eqv
equivalence infix operator, all wrapped inside a none()
Junction.
Although not necessary for this task, I have used eqv
instead of ==
so that the derangements()
function also works with any set of arbitrary objects (eg. strings, lists, etc.)
Output:
Last updated