Eek Code Snippets
Eek Code Snippets
In order to get some more experience with Eek's look&feel, I started to reimplement small functions written in other languages. Preferable those that allow me to use Eek's advanced features. For example
the famous Haskell implementation of Quicksort (intended to show how easy Haskell is, but more likely to be used as proof that the functional programming guys are.. special):
qsort [] = []
qsort (x:xs) = qsort elts_lt_x ++ [x] ++ qsort elts_greq_x
where
elts_lt_x = [y | y <- xs, y < x]
elts_greq_x = [y | y <- xs, y >= x]
In Eek, using the beautiful closure filter operator '.()', a quicksort implementation could look like
class QuickSort
static List<int> sort(List<int> list)
int s = list[list.size / 2]
return sort(list.(it <= s)) + sort(list.(it > s))
end
.
This is an Eek version of the
Binary Insertion Sort algorithm in Jeffrey Stedfast's blog (first example of the entry):
class BinarySearch
static (int mid) sort(List a, int low, int high, int key)
int range = low + high
mid = (range + 1) / 2
if (range % 2) == 1
mid -= 1
if key > a[mid]
return sort(a, mid + 1, high, key)
else if key < a[mid]
return sort(a, low, mid, key)
return
end
Nothing special here, just made the 'mid' variable a named return parameter.
More examples will follow.
written at 01:24. (0 Comments, Permalink)