14.07.2013 Views

Contents - Cultural View

Contents - Cultural View

Contents - Cultural View

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Quark Framework 259<br />

has proven to provide a good balance of flexibility and 'directness' when interfacing with external operations.<br />

One of the main design goals for CAL was to make the language as comfortable as possible for mainstream<br />

developers to pick up and use effectively. This is reflected in choices for syntax, but also in conventions and patterns<br />

used within the standard libraries. For example, libraries use longer, descriptive names and are commented to explain<br />

the implementations and best practices for use.<br />

To see some CAL language source code click here [3] . This tutorial CAL module is designed as a top-to-bottom<br />

'feature parade' to showcase basic syntax with examples of some built-in and user defined data structures.<br />

Here are a few examples, derived from the tutorial module linked to in the preceding paragraph:<br />

1. Quicksort<br />

/**<br />

* Here is a simple implementation of the quicksort algorithm for lists in<br />

* CAL.<br />

*<br />

* Note: it is not the most efficient implementation, since it filters the<br />

* list twice to partition.<br />

*<br />

* It is used here as an illustration. The production implementation of<br />

* sorting on lists is<br />

* {@link List.sort@}.<br />

*<br />

* The type of quicksort is constrained by the {@link Ord@} type class. This<br />

* means that quicksort can sort list of any orderable type.<br />

*/<br />

quicksort :: Ord a => [a] -> [a];<br />

quicksort list =<br />

let<br />

in<br />

//partition_min is a local function of 1 argument<br />

partition_min pivot = filter (\x -> x < pivot);<br />

partition_max pivot = filter (\x -> x >= pivot);<br />

case list of<br />

[] -> [];<br />

pivot : tail -><br />

;<br />

quicksort (partition_min pivot tail)<br />

++ (pivot : (quicksort (partition_max pivot tail)));<br />

Note that CAL supports inline documentation comments, with embedded tags. Function type declarations<br />

immediately precede the function definition. CAL supports type classes (e.g. Ord). Two local functions are declared<br />

in the let block. Quicksort has a recursive definition, building up the output list at each level of recursion from the<br />

sorts applied to the list of values either side of the pivot item.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!