| 1 |
abate |
260 |
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
|
| 2 |
abate |
341 |
<page name="examples">
|
| 3 |
abate |
260 |
|
| 4 |
abate |
341 |
<title>Code examples</title>
|
| 5 |
abate |
260 |
|
| 6 |
abate |
341 |
<left>
|
| 7 |
|
|
<p>This page presents some simple CDuce programs.</p>
|
| 8 |
|
|
<boxes-toc/>
|
| 9 |
|
|
<p>See also:</p>
|
| 10 |
|
|
<local-links href="index,proto,memento"/>
|
| 11 |
|
|
</left>
|
| 12 |
abate |
260 |
|
| 13 |
|
|
<box title="Our canonical example" link="sort">
|
| 14 |
abate |
350 |
<p>
|
| 15 |
|
|
The example below is the one we use to demonstrate
|
| 16 |
|
|
how overloaded functions can avoid duplicating code.
|
| 17 |
|
|
Without overloaded functions, we would need to define
|
| 18 |
|
|
two mutually recursive functions in order to type-check
|
| 19 |
|
|
the transformation. Here, two constraints
|
| 20 |
|
|
in the (highlighted) function interface
|
| 21 |
|
|
can express precisely the behaviour of the function.
|
| 22 |
|
|
</p>
|
| 23 |
abate |
341 |
<sample>
|
| 24 |
abate |
260 |
<include-verbatim file="overloading.cd"/>
|
| 25 |
abate |
341 |
</sample>
|
| 26 |
abate |
260 |
</box>
|
| 27 |
|
|
|
| 28 |
abate |
267 |
<box title="The script that generates this site" link="site">
|
| 29 |
abate |
350 |
<p>
|
| 30 |
|
|
The script below is one of the longest CDuce application ever written
|
| 31 |
|
|
;-) It is used to produce all the pages of this web site (except
|
| 32 |
|
|
the <local href="proto">web prototype</local> which is
|
| 33 |
abate |
357 |
a CGI script written in OCaml). CDuce type system ensures
|
| 34 |
abate |
350 |
that produced pages are valid w.r.t XHTML 1.0 Strict.
|
| 35 |
|
|
</p>
|
| 36 |
|
|
<p>
|
| 37 |
|
|
This program features both XML and text-content manipulation.
|
| 38 |
|
|
It also demonstrates the use of non-XML internal data structures.
|
| 39 |
|
|
Here, a tree represents internally the site
|
| 40 |
|
|
structure, and a list represents the path from the
|
| 41 |
|
|
root to the current page (in order to display the "You're here" line).
|
| 42 |
|
|
</p>
|
| 43 |
abate |
344 |
<sample highlight="false">
|
| 44 |
abate |
260 |
<include-verbatim file="site.cd"/>
|
| 45 |
abate |
341 |
</sample>
|
| 46 |
abate |
260 |
</box>
|
| 47 |
|
|
|
| 48 |
|
|
</page>
|
| 49 |
|
|
|