[exprla-devel] [xpl] Adding up a list of numbers, check it out.
Status: Pre-Alpha
Brought to you by:
xpl2
From: reid_spencer <ras...@re...> - 2002-01-31 08:11:12
|
--- In xpl-dev@y..., Jonathan Burns <saski@w...> wrote: <xpl> <comment> This line is mandatory. </comment> <include content="xpl.dtd" version="x.x"/ > <comment> This includes a pre-existing data type, an XML document with the structure containing a list of integers: <List> [ <int> string </int> ]* </List> Within the program, an element of this form will be referred to by the name "intList". </comment> <include intList="list-of-int.dtd"> <comment> Here we define a program variable to contain a list of integers. <comment> <variable type=intList> <name> mylist </name> </variable> <comment> We define an integer variable> </comment> <variable type=int> <name> total </name> </variable> <comment> We define a general-purpose element </comment> <variable type=element> <name> index </name> </variable> <comment> We define "abort" as a macro for an action to take on error. </comment> <set> abort <stdout> <data> <List> zero </List> </data> </stdout> </set> <comment> At last, the action commences! We read in the intList data. </comment> <stdin> mylist </stdin> <if> <false> <verify> mylist </verify> </false> abort <comment> Handy things, macros. </comment> </if> <comment> We set the general element "index" to refer to mylist as a whole. But we can think of this as referring to the root of the mylist. (Compare directory commands: think of this as setting up "index" as a symbolic link to mylist.) N.B. "set" is used here as a full copy assignment, with all children. It may be wasteful in some cases. </comment> <set> index mylist </set> <comment> Check whether mylist is empty. </comment> <if> <equals> <number-of-children> mylist </number-of-children> zero <equals> abort <else> <comment> Shift "index" down, to refer to "mylist"'s first child. (Compare change-directory command.) </comment> <set> index <first-child> index <comment> This could as well be "mylist". </comment> </first-child> </set> <comment> Initialize our "total" variable. </comment> <set> total <data> <int> zero </int> <data> </set> <comment> And this is what does the work: a Do-While loop which adds the list elements to our "total" variable. N.B. It doesn't make sense to add up strings, even strings like "9848001" and "0". But it does make sense to add up elements such as <int> 9848001 </int> </comment> <do> <add> total index </add> <comment> Here's the While condition </comment> <not> <equals> index <last-child> mylist <last-child> </equals> </do> </else> </if> <comment> And we're out, with the list elements added up in "total". So, output it. </comment> <stdout> <data> <List> total </List> </data> </stdout> <comment> And take the rest of the day off. </comment> </xpl> --- End forwarded message --- |