| 1 |
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
|
| 2 |
<page name="examples">
|
| 3 |
|
| 4 |
<title>Advanced examples</title>
|
| 5 |
|
| 6 |
<left>
|
| 7 |
<p>This page presents some sample CDuce programs.</p>
|
| 8 |
<boxes-toc/>
|
| 9 |
<p>See also:</p>
|
| 10 |
<local-links href="index,proto,memento"/>
|
| 11 |
</left>
|
| 12 |
|
| 13 |
|
| 14 |
<box title="Advanced examples" link="start">
|
| 15 |
|
| 16 |
This page presents some advanced programming examples in CDuce. <b
|
| 17 |
style="color:#FF0080">If you never saw CDuce programs before, this is the wrong
|
| 18 |
page</b> to start with. Rather follow our <local href="tutorial"/>, or test the
|
| 19 |
simple examples in our <a href="/cgi-bin/cduce">on line demo</a>.
|
| 20 |
|
| 21 |
</box>
|
| 22 |
|
| 23 |
|
| 24 |
<box title="Our canonical example" link="sort">
|
| 25 |
<p>
|
| 26 |
The example below is the one we use to demonstrate
|
| 27 |
how overloaded functions can avoid duplicating code.
|
| 28 |
Without overloaded functions, we would need to define
|
| 29 |
two mutually recursive functions in order to type-check
|
| 30 |
the transformation. Here, two constraints
|
| 31 |
in the (highlighted) function interface
|
| 32 |
can express precisely the behaviour of the function.
|
| 33 |
A detailed explanation of the code can be found <a href="tutorial_overloading.html#canonical">here</a>.
|
| 34 |
</p>
|
| 35 |
<sample>
|
| 36 |
<include-verbatim file="overloading.cd"/>
|
| 37 |
</sample>
|
| 38 |
</box>
|
| 39 |
|
| 40 |
<box title="Datatypes + first-class functions" link="data">
|
| 41 |
<p>
|
| 42 |
The program below shows how to simulate ML data types in CDuce.
|
| 43 |
It implements a (naive backtracking) regular expression recognizer.
|
| 44 |
The examples also demonstrate the use of first-class functions
|
| 45 |
(used as continuations).
|
| 46 |
</p>
|
| 47 |
<p>
|
| 48 |
Exercise for the reader: show that the algorithm may not terminate for
|
| 49 |
some special regular expressions.
|
| 50 |
</p>
|
| 51 |
<sample highlight="false">
|
| 52 |
<include-verbatim file="regexp.cd"/>
|
| 53 |
</sample>
|
| 54 |
</box>
|
| 55 |
|
| 56 |
|
| 57 |
<box title="CDuce quine" link="quine">
|
| 58 |
<p>
|
| 59 |
A quine (a.k.a. self-rep) is a program that produces its own source
|
| 60 |
code. Here is an example of a quine written in CDuce.
|
| 61 |
</p>
|
| 62 |
<sample highlight="false">
|
| 63 |
<include-verbatim file="quine.cd"/>
|
| 64 |
</sample>
|
| 65 |
</box>
|
| 66 |
|
| 67 |
<box title="The script that generates this site" link="site">
|
| 68 |
<p>
|
| 69 |
The script below is one of the longest CDuce application ever written
|
| 70 |
;-) It is used to produce all the pages of this web site (except
|
| 71 |
the <local href="proto">web prototype</local> which is
|
| 72 |
a CGI script written in OCaml). CDuce type system ensures
|
| 73 |
that produced pages are valid w.r.t XHTML 1.0 Strict.
|
| 74 |
</p>
|
| 75 |
<p>
|
| 76 |
This program features both XML and text-content manipulation.
|
| 77 |
It also demonstrates the use of non-XML internal data structures.
|
| 78 |
Here, a tree represents internally the site
|
| 79 |
structure, and a list represents the path from the
|
| 80 |
root to the current page (in order to display the "You're here" line).
|
| 81 |
</p>
|
| 82 |
<sample highlight="false">
|
| 83 |
<include-verbatim file="site.cd"/>
|
| 84 |
</sample>
|
| 85 |
</box>
|
| 86 |
|
| 87 |
</page>
|
| 88 |
|