Re: [xsl] XPath 3.0: Is it possible to do recursion in an anonymous function?

Subject: Re: [xsl] XPath 3.0: Is it possible to do recursion in an anonymous function?
From: Dimitre Novatchev <dnovatchev@xxxxxxxxx>
Date: Sat, 13 Oct 2012 12:37:24 -0700
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.

Current Thread