[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: XPath 3.0: Is it possible to do recursion in an a

Subject: Re: XPath 3.0: Is it possible to do recursion in an anonymous function?
From: Michael Kay <mike@xxxxxxxxxxxx>
Date: Sun, 14 Oct 2012 09:03:46 +0100
Re:  XPath 3.0: Is it possible to do recursion in an  a
It's nice to know that this can be done, but I do wonder if it isn't simpler to use a named function instead?

Michael Kay
Saxonica

On 14/10/2012 00:01, Dimitre Novatchev wrote:
Finally, it is possible to hide the use of the second argument -- like this:


<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs"> <xsl:output method="text"/>

   <xsl:template match="/">
      <xsl:sequence select=
        "let $f := function($n as xs:integer,
                                          $f1 as function(xs:integer,
function()) as xs:integer
                                        ) as xs:integer
                         {if($n eq 0)
                             then 1
                             else $n * $f1($n -1, $f1)
                         },
                $F := function($n as xs:integer) as xs:integer
                           {$f($n, $f)}
          return
             $F(5)
        "/>
    </xsl:template>
  </xsl:stylesheet>


Cheers, Dimitre

On Sat, Oct 13, 2012 at 12:51 PM, Dimitre Novatchev
<dnovatchev@xxxxxxxxx> wrote:
Boh my previous answers had a slight issue (that didn't prevent Saxon
from executing them and producing the correct result) with the typing
of the "mock" function parameter $f1.

Here is this corrected:

<xsl:stylesheet  version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:xs="http://www.w3.org/2001/XMLSchema"
   exclude-result-prefixes="xs">
   <xsl:output method="text"/>

   <xsl:template match="/">
      <xsl:sequence select=
        "let $f := function($n as xs:integer,
                                    $f1 as function(xs:integer,
function()) as xs:integer
                                    ) as xs:integer
                         {if($n eq 0)
                             then 1
                             else $n * $f1($n -1, $f1)
                         }
          return
             $f(5, $f)
        "/>
    </xsl:template>
  </xsl:stylesheet>


Cheers, Dimitre

On Sat, Oct 13, 2012 at 12:47 PM, Dimitre Novatchev
<dnovatchev@xxxxxxxxx> wrote:
Here is an equivalent XSLT-based solution -- i both cases there is no
literal function defined -- just an inline one:

<xsl:stylesheet  version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:xs="http://www.w3.org/2001/XMLSchema"
   exclude-result-prefixes="xs">
   <xsl:output method="text"/>

   <xsl:template match="/">
      <xsl:sequence select=
        "let $f := function($n as xs:integer,
        $f1 as function(xs:integer) as xs:integer
        ) as xs:integer
        {if($n eq 0)
        then 1
        else $n * $f1($n -1, $f1)
        }
        return
        $f(5, $f)
        "/>
    </xsl:template>
  </xsl:stylesheet>

Again, the correct result is produced:

120


Cheers, Dimitre

On Sat, Oct 13, 2012 at 12:37 PM, Dimitre Novatchev
<dnovatchev@xxxxxxxxx> wrote:
You are right, but there is an easy workaround.

Here is an example with an in-line factorial function:


declare option saxon:output "omit-xml-declaration=yes"; let $f := function($n as xs:integer, $f1 as function(xs:integer) as xs:integer ) as xs:integer {if($n eq 0) then 1 else $n * $f1($n -1, $f1) } return $f(5, $f)

The result we get is correct:

120


Cheers, Dimitre

On Sat, Oct 13, 2012 at 12:03 PM, Costello, Roger L. <costello@xxxxxxxxx> wrote:
Hi Folks,

Is it possible to do recursion in an anonymous function?

Example: I would like to implement an "until" function. It has three arguments:

1. p is a boolean function
2. f is a function on x
3. x is the value being processed

Read the following function call as: decrement 3 until it is negative
       $until ($isNegative, $decrement, 3)

where
       $isNegative := function($x as xs:integer) {$x lt 0}
       $decrement := function($x as xs:integer) {$x - 1}

Here's how I attempted to implement function until:

$until := function(
                                      $p as function(item()*) as xs:boolean,
                                      $f as function(item()*) as item()*,
                                      $x as item()*
                                 ) as item()*
                          {
                              if ($p($x)) then
                                    $x
                              else
                                    $until($p, $f, $f($x))  <-- RECURSE ...THIS IS NOT ALLOWED, I THINK
                            }

Is there a way to implement function until in XPath 3.0? (I know how to implement it in XSLT 3.0)

/Roger



--
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
-------------------------------------
To avoid situations in which you might make mistakes may be the
biggest mistake of all
------------------------------------
Quality means doing it right when no one is looking.
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play
-------------------------------------
Facts do not cease to exist because they are ignored.
-------------------------------------
Typing monkeys will write all Shakespeare's works in 200yrs.Will they
write all patents, too? :)
-------------------------------------
I finally figured out the only reason to be alive is to enjoy it.


--
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
-------------------------------------
To avoid situations in which you might make mistakes may be the
biggest mistake of all
------------------------------------
Quality means doing it right when no one is looking.
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play
-------------------------------------
Facts do not cease to exist because they are ignored.
-------------------------------------
Typing monkeys will write all Shakespeare's works in 200yrs.Will they
write all patents, too? :)
-------------------------------------
I finally figured out the only reason to be alive is to enjoy it.


--
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
-------------------------------------
To avoid situations in which you might make mistakes may be the
biggest mistake of all
------------------------------------
Quality means doing it right when no one is looking.
-------------------------------------
You've achieved success in your field when you don't know whether what
you're doing is work or play
-------------------------------------
Facts do not cease to exist because they are ignored.
-------------------------------------
Typing monkeys will write all Shakespeare's works in 200yrs.Will they
write all patents, too? :)
-------------------------------------
I finally figured out the only reason to be alive is to enjoy it.

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.