Anagrams

An anagram is a word or phrase made by rearranging the letters in another word or phrase. Making anagrams is a task best solved with a recursive algorithm and a 'natural' for LISP. The following two algorithms take a string and return a list of all possible permutations.

The first of the two algorithms was contributed by Sam Cox and works directly on the string itself. Recursively subsections of the string are exploded, rotated and than joined to form a new string.:

The second algorithm is a bit slower but builds on a generally applicable permutations algorithm. The permutations function generates all possible permuations of offsets into the string, then applies those permuations: