examples.xml 2.89 KB
Newer Older
Giuseppe Castagna's avatar
Giuseppe Castagna committed
1
<?xml version="1.0" encoding="UTF-8" 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 9 10 11
<boxes-toc/>
<p>See also:</p>
<local-links href="index,proto,memento"/>
</left>
12

13 14 15 16 17 18

<box title="Advanced examples" link="start">

This page presents some advanced programming examples in CDuce. <b
style="color:#FF0080">If you never saw CDuce programs before, this is the wrong
page</b> to start with. Rather follow our <local href="tutorial"/>, or test the
19
simple examples in our <a href="http://reglisse.ens.fr/cgi-bin/cduce">on line demo</a>.
20 21 22 23

</box>


24
<box title="Our canonical example" link="sort">
25 26 27 28 29 30 31 32
<p>
The example below is the one we use to demonstrate
how overloaded functions can avoid duplicating code.
Without overloaded functions, we would need to define
two mutually recursive functions in order to type-check
the transformation. Here, two constraints
in the (highlighted) function interface
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 39
</box>

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
<box title="Datatypes + first-class functions" link="data">
<p>
The program below shows how to simulate ML data types in CDuce.
It implements a (naive backtracking) regular expression recognizer.
The examples also demonstrate the use of first-class functions
(used as continuations).
</p>
<p>
Exercise for the reader: show that the algorithm may not terminate for 
some special regular expressions.
</p>
<sample highlight="false">
<include-verbatim file="regexp.cd"/>
</sample>
</box>

56 57 58 59 60 61 62 63 64
<box title="First-class functions and XML together" link="funxml">
<p>
The program below illustrates the use of first-class functions stored
in (pseudo) XML documents.
</p>
<sample highlight="false">
<include-verbatim file="funxml.cd"/>
</sample>
</box>
65

66 67 68 69 70 71 72 73 74
<box title="CDuce quine" link="quine">
<p>
A quine (a.k.a. self-rep) is a program that produces its own source
code. Here is an example of a quine written in CDuce.
</p>
<sample highlight="false">
<include-verbatim file="quine.cd"/>
</sample>
</box>
75

76
<box title="The script that generates this site" link="site">
77 78 79 80
<p>
The script below is one of the longest CDuce application ever written
;-) It is used to produce all the pages of this web site (except
the <local href="proto">web prototype</local> which is
81
a CGI script written in OCaml). CDuce type system ensures
82 83 84 85 86 87 88 89 90
that produced pages are valid w.r.t XHTML 1.0 Strict.
</p>
<p>
This program features both XML and text-content manipulation.
It also demonstrates the use of non-XML internal data structures.
Here, a tree represents internally the site
structure, and a list represents the path from the
root to the current page (in order to display the "You're here" line).
</p>
91
<sample highlight="false">
92
<include-verbatim file="site.cd"/>
93
</sample>
94 95 96 97
</box>

</page>