Anybody knows a purely functional algorithm for finding the next permutation of a sequence (in lexicographic order)?