|
[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: Functional program for "list sum"
On 6/10/05, Mukul Gandhi <mukul_gandhi@xxxxxxxxx> wrote:
> Hello Dimitre,
> Can I hope to get response from you ..
>
> Regards,
> Mukul
Hi Mukul,
There are good books on Functional programming and I am not at all an
authority in this field.
If you really need to look at what I've written on this subject (in
relation to FXSL), one source is available at:
http://www.mulberrytech.com/Extreme/Proceedings/xslfo-pdf/2003/Novatchev01
/EML2003Novatchev01.pdf
As for an example of a function that calculates the sum of the
elements of a list, do have a look at how this is done in FXSL.
As one might have expected, this is a simple application of the foldl
function, which in Haskell can be written as:
sum xs = foldl (+) 0 xs
Now, as an exercise, think how to define a function that calculates
the product of all elements of a list :o)
Here's the defiition of f:sum() from FXSL:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:f="http://fxsl.sf.net/"
xmlns:sum-fold-func="sum-fold-func"
exclude-result-prefixes="f xs sum-fold-func"
>
<xsl:import href="func-foldl.xsl"/>
<xsl:function name="f:sum">
<xsl:param name="pList"/>
<xsl:variable name="sum-fold-func"
select="document('')/*/sum-fold-func:*[1]"/>
<xsl:sequence select="f:foldl($sum-fold-func, 0, $pList)"/>
</xsl:function>
<sum-fold-func:sum-fold-func/>
<xsl:template name="add" match="sum-fold-func:*"
mode="f:FXSL">
<xsl:param name="arg1" select="0"/>
<xsl:param name="arg2" select="0"/>
<xsl:value-of select="$arg1 + $arg2"/>
</xsl:template>
</xsl:stylesheet>
Cheers,
Dimitre Novatchev
> -------------------------------------------------
> "Two parallel lines touch each other at infinity"
> -------------------------------------------------
>
> --- Mukul Gandhi <mukul_gandhi@xxxxxxxxx> wrote:
>
> > I am trying to implement a "integer sum" function in
> > a
> > functional manner .. My stylesheet so far is -
> >
> > <?xml version="1.0"?>
> > <xsl:stylesheet
> > xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> >
> > xmlns:xs="http://www.w3.org/2001/XMLSchema"
> > xmlns:fn="http://whatever"
> > version="1.0">
> >
> > <xsl:output method="text" />
> >
> > <xsl:template match="/">
> > <xsl:variable name="result"
> > select="fn:listSum(null,(1,2,3))" />
> > <xsl:value-of select="$result" />
> > </xsl:template>
> >
> > <xsl:function name="fn:listSum" as="xs:integer">
> > <xsl:param name="num" as="xs:integer" />
> > <xsl:param name="list" as="xs:integer+" />
> > <xsl:sequence
> > select="fn:listSum($num,$list[position() > 1])"
> > />
> > </xsl:function>
> >
> > </xsl:stylesheet>
> >
> > But I am getting following error with Saxon 8.4 ..
> >
> > "Warning: Running an XSLT 1.0 stylesheet with an
> > XSLT
> > 2.0 processor
> > Error on line 10 of file:/C:/xml/xsleg/xslt/fp.xsl:
> > XPTY0004: An empty sequence is not allowed as the
> > first argument of fn:listSum
> > ()
> > Transformation failed: Run-time errors were
> > reported"
> > ..
> >
> > Can someone please help me ..?
> >
> > Regards,
> > Mukul
> >
> >
> >
> > __________________________________
> > Discover Yahoo!
> > Have fun online with music videos, cool games, IM
> > and more. Check it out!
> > http://discover.yahoo.com/online.html
> >
> >
>
>
>
>
> __________________________________
> Discover Yahoo!
> Have fun online with music videos, cool games, IM and more. Check it out!
> http://discover.yahoo.com/online.html
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|

Cart








