[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Functional Programming: How do I convert an xsl:for-each loop into a functional style?


Subject: Re: [xsl] Functional Programming: How do I convert an xsl:for-each loop into a functional style?
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Thu, 21 Jan 2010 06:16:00 -0800

> My understanding is that a litmus test for whether a program is written in a
functional style is whether it can be executed in any order.

This is a large exaggeration. If there are functions foo() and bar(),
then in the expression:

   foo(bar(x))

foo() cannot be evaluated before bar(x) has been evaluated.


In fact, there are many examples of expressions that require quite
strict sequential evaluation. This demonstrates the power of FP that
an expression can be used to specify both no-sequential and (quite)
sequential processing.

Finally, the proposed solution of the partial sums problem is very
inefficient -- O(N^2)). For a solution with linear complexity use the
f:scanl() function of FXSL.

--
Cheers,
Dimitre Novatchev
---------------------------------------
Truly great madness cannot be achieved without significant intelligence.
---------------------------------------
To invent, you need a good imagination and a pile of junk
-------------------------------------
Never fight an inanimate object
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play



On Thu, Jan 21, 2010 at 4:38 AM, Costello, Roger L. <costello@xxxxxxxxx>
wrote:
> Hi Folks,
>
> My question is about writing XSLT in a functional programming style.
>
> My understanding is that a litmus test for whether a program is written in a
functional style is whether it can be executed in any order.
>
> Consider this problem: convert a sequence of N numbers into a cumulative
sequence, in which each number is the sum of the previous numbers.
>
> Here is a loop that solves the problem:
>
> <xsl:for-each select="Number">
> B  B  B <xsl:value-of select="sum((., preceding-sibling::Number))" />
> </xsl:for-each>
>
> Can this code be executed in any order?
>
> Suppose the loop is unraveled into a series of <xsl:value-of> statements. If
those statements are evaluated in arbitrary order then the output would not be
in the desired sequence. B Thus, I conclude, this loop is not written in a
functional style. Do you agree?
>
> How would the problem be solved in a functional style?
>
> /Roger


Current Thread
Keywords