Commit 72c8993c authored by Pietro Abate's avatar Pietro Abate
Browse files

[r2003-07-04 17:57:45 by cvscast] Empty log message

Original author: cvscast
Date: 2003-07-04 17:57:45+00:00
parent f53a4cfc
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<page name="tutorial2">
<title>First functions</title>
<banner>First functions</banner>
<left>
<boxes-toc/>
</left>
<box title="First functions" link="t2">
<p>
A first example of transformation is <code>names</code>, which extracts the
sequences of all names of parents in a <code>ParentBook</code> element:
</p>
<sample><![CDATA[
let names (ParentBook -> [Name*])
<parentbook>x -> (map x with <person>[ n _*] -> n)
]]></sample>
<p>
The name of the transformation is followed by an <i>interface</i> that
states that <code>names</code> is a function from
<code>ParentBook</code> elements to (possibly empty) sequences of
<code>Name</code> elements. This is obtained by matching the argument of the
function against the pattern
</p>
<sample><![CDATA[<parentbook>x ]]></sample>
<p>which binds <code>x</code> to
the sequence of person elements forming the parentbook. The operator
<code>map</code> applies to each element of a sequence (in this case <code>x</code>) the
transformation defined by the subsequent pattern matching. Here <code>map</code>
returns the sequence obtained by replacing each person in <code>x</code> by its
<code>Name</code> element. Note that we use the pattern
</p>
<sample><![CDATA[<person>[ n _*],
]]></sample>
<p>to match the person elements: <code>n</code> matches (and captures) the <code>Name</code>
element-that is, the first element of the sequence-, <code>_*</code> matches (and discards) the sequence of elements that
follow, and person matches the tag of the person (although the
latter contains an attribute). The interface and the type definitions
ensure that the tags will be the expected ones, so we could optimize the
code by defining a body that skips the check of the tags:
</p>
<sample><![CDATA[
<_> x -> (map x with <_>[ n _*] -> n)
]]></sample>
</box>
</page>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment