I love it! Glad to see this happening. I can't wait for the hard stuff.
One comment - though you are focusing on Haskell, please keep it tied to C# (e.g., "here's how to do it in C#") and keep it practical. By comign back to C#, unfamiliar concepts look familiar. By keeping it practical, we'll see how this stuff can really be
used. Haskell - not just for Fibonacci anymore! OK that's a bit facetious.
As for the homework, here is my solution. I basically transcribed the Haskell version shown. I am sure it can be prettier:
public static IEnumerable<A> QuickSort<A>(IEnumerable<A> vals)
where A : IComparable<A>
{
if (vals.Skip(1).IsEmpty())
return vals;
else
{
A pivot = vals.First();
var rest = vals.Skip(1);
var left = from x in rest
where x.CompareTo(pivot) <= 0
select x;
var right = from x in rest
where x.CompareTo(pivot) > 0
select x;
return QuickSort(left).Concat(vals.Take(1)).Concat(right);
}
}
Note : IsEmpty is an extension method which tests if a sequence is empty. I can't believ that is not already defiend in the framework so I am sure i missed it.
I particularly like the end part, where I concatenate the single pivot value into the sequence:
return QuickSort(left).Concat(vals.Take(1)).Concat(right);
Since the list is not empty, I know I can take the first value and stick in the right place. My base case also takes advantage here:
if (vals.Skip(1).IsEmpty())
return vals;
else
...
If the sequence given only has one element, I just return it unchanged and it automatically goes in the right spot. Thank you recursion!
Full source code & VS2008 project available on
github.
Justin