static IterableIterator> allPermutations_iterator(L theList) { ret iteratorFromFunction(new F0 { L list = cloneList(theList); int[] stack = new int[l(theList)+1]; int n = l(theList); A get() { findNext(); if (n <= 1) { ++n; // return from subroutine ret cloneList(list); } else if (stack[n] < n) ++stack[n]; } L l = cloneList(list); if (stackPtr[n] < n) ++stackPtr[n]; ret l; } }; new LL out; allPermutations_impl(cloneList(l), l(l), out); ret out; } static void allPermutations_impl(L l, int n, LL out) { if (n <= 1) { ping(); out.add(cloneList(l)); } else for(int i = 0; i < n; i++) { allPermutations_impl(l, n-1, out); swapElements(l, n % 2 == 0 ? i : 0, n-1); } }