## 趣味编程：函数式链表的快速排序

2009-08-27 17:50  Jeffrey Zhao  阅读(10759)  评论(88编辑  收藏  举报

qsort [] = []
qsort (x:xs) = qsort (filter (< x) xs) ++ [x] ++ qsort (filter (>= x) xs)

public class ImmutableList<T> : IEnumerable<T>
{
public readonly static ImmutableList<T> Empty = new ImmutableList<T>(default(T));

{
this.Tail = tail;
}

public T Head { get; private set; }

public ImmutableList<T> Tail { get; private set; }

...
}


public class ImmutableList<T> : IEnumerable<T>
{
...

#region IEnumerable<T> Members

public IEnumerator<T> GetEnumerator()
{
var current = this;
while (current != Empty)
{
current = current.Tail;
}
}

#endregion

#region IEnumerable Members

IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}

#endregion
}


qsort [] = []
qsort (x:xs) = ...

public static class ImmutableListExtensions
{
public static ImmutableList<T> QuickSort<T>(this ImmutableList<T> list, Func<T, T, int> compare)
{
if (list == null) throw new ArgumentNullException("list");
if (compare == null) throw new ArgumentNullException("compare");

if (list == ImmutableList<T>.Empty)
{
...
}
else
{
...
}
}
}


qsort (x:xs) = qsort (filter (< x) xs) ++ [x] ++ qsort (filter (>= x) xs)