Skip to content

Instantly share code, notes, and snippets.

@mikeminutillo
Created October 27, 2011 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikeminutillo/1319475 to your computer and use it in GitHub Desktop.
Save mikeminutillo/1319475 to your computer and use it in GitHub Desktop.
Permutations - writing LISP in C#
public static class Extensions
{
public static IEnumerable<IEnumerable<T>> Permute<T>(this IEnumerable<T> items)
{
var isEmpty = true;
foreach (var item in items)
{
isEmpty = false;
foreach (var p in items.Except(item.Yield()).Permute())
yield return item.Yield().Concat(p);
}
if (isEmpty)
yield return Enumerable.Empty<T>();
}
public static IEnumerable<T> Yield<T>(this T item)
{
yield return item;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment