all tasks

This commit is contained in:
Ingy döt Net 2013-04-11 01:07:29 -07:00
parent b83f433714
commit 68f8f3e56b
14735 changed files with 178959 additions and 0 deletions

View file

@ -0,0 +1,43 @@
using System;
using System.Console;
module Bubblesort
{
Bubblesort[T] (x : list[T]) : list[T]
where T : IComparable
{
def isSorted(y)
{
|[_] => true
|y1::y2::ys => (y1.CompareTo(y2) < 0) && isSorted(y2::ys)
}
def sort(y)
{
|[y] => [y]
|y1::y2::ys => if (y1.CompareTo(y2) < 0) y1::sort(y2::ys)
else y2::sort(y1::ys)
}
def loop(y)
{
if (isSorted(y)) y else {def z = sort(y); loop(z)}
}
match(x)
{
|[] => []
|_ => loop(x)
}
}
Main() : void
{
def empty = [];
def single = [2];
def several = [2, 6, 1, 7, 3, 9, 4];
WriteLine(Bubblesort(empty));
WriteLine(Bubblesort(single));
WriteLine(Bubblesort(several));
}
}

View file

@ -0,0 +1,33 @@
using System;
using System.Console;
module Bubblesort
{
public static Bubblesort[T](this x : array[T]) : void
where T : IComparable
{
mutable changed = false;
def ln = x.Length;
do
{
changed = false;
foreach (i in [0 .. (ln - 2)])
{
when (x[i].CompareTo(x[i + 1]) > 0)
{
x[i] <-> x[i + 1];
changed = true;
}
}
} while (changed);
}
Main() : void
{
def several = array[2, 6, 1, 7, 3, 9, 4];
several.Bubblesort();
foreach (i in several)
Write($"$i ");
}
}