Re: Recursion in XPath 3.1 inline function definition

Hi,


there are also xidel <https://videlibri.de/cgi-bin/xidelcgi>

example 
<https://videlibri.de/cgi-bin/xidelcgi?&data=%3Cinput%2F%3E&=%3Cinput%2F%3E&extract=%0Alet%20%24fact-base%20%3A%3D%20function(%24n%20as%20xs%3Ainteger%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24f1%20as%20function(xs%3Ainteger%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20function(*))%20as%20xs%3Ainteger%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20)%20as%20xs%3Ainteger%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%7Bif(%24n%20eq%200)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20then%201%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20else%20%24n%20*%20%24f1(%24n%20-1%2C%20%24f1)%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%24Fact%20%3A%3D%20function(%24n%20as%20xs%3Ainteger)%20as%20xs%3Ainteger%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%24fact-base(%24n%2C%20%24fact-base)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20return%0A%20%20%20%20%20%20%20%20%20%20%20%24Fact(5)%0A&=&input-format=auto&printed-node-format=text&output-format=adhoc&compatibility=Standard%20XQuery&dot-notation=off&extract-kind=xpath3.1&no-extended-strings=true&no-json=true&no-json-literals=true&only-json-objects=true&strict-type-checking=true&strict-namespaces=true&case-sensitive=true>



Sincerely,
Benito

On 29.03.25 15:52, Martin Honnen wrote:
>
>
> Am 29.03.2025 um 15:39 schrieb Dimitre Novatchev:
>> There is a way to use recursion with anonymous (inline) functions in 
>> pure XPath 3, and I reported it in my blog and at the Balisage 2013 
>> conference.
>> Since then, I have built my own, pure XPath 3.1 function libraries 
>> using this technique.
>>
>> Here is a simple example:
>>
>> let $fact-base := 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)
>>
>>               },
>>     $Fact := function($n as xs:integer) as xs:integer
>>             {
>>                 $fact-base($n, $fact-base)
>>             }
>>
>>    return
>>            $Fact(5)
>>
>> One can run it with BaseX or with Oxygen, or directly online with 
>> BaseX Fiddle: https://fiddle.basex.org/.

>
>
> XPath 3.1 online evaluation is also possible at the SaxonJS powered 
> XPath 3.1 fiddle <https://martin-honnen.github.io/xpath31fiddle/>, 
> example fiddle 
> <https://martin-honnen.github.io/xpath31fiddle/?xpath=let+%24fact-base+%3A%3D+function%28%24n+as+xs%3Ainteger%2C%0D%0A+++++++++++++++++++%24f1+as+function%28xs%3Ainteger%2C%0D%0A+++++++++++++++++++function%28*%29%29+as+xs%3Ainteger%0D%0A+++++++++++++++++++%29+as+xs%3Ainteger%0D%0A+++++++++++++%7Bif%28%24n+eq+0%29%0D%0A+++++++++++++++++then+1%0D%0A+++++++++++++++++else+%24n+*+%24f1%28%24n+-1%2C+%24f1%29%0D%0A%0D%0A++++++++++++++%7D%2C%0D%0A++++%24Fact+%3A%3D+function%28%24n+as+xs%3Ainteger%29+as+xs%3Ainteger%0D%0A++++++++++++%7B%0D%0A++++++++++++++++%24fact-base%28%24n%2C+%24fact-base%29%0D%0A++++++++++++%7D%0D%0A%0D%0A+++return%0D%0A+++++++++++%24Fact%285%29&input=&input-type=None>, 
> and the CheerpJ and Saxon 12 powered XML workbench 
> <https://martin-honnen.github.io/cheerpj3-saxonhe12-fiddle/>, example 
> fiddle 
> <https://martin-honnen.github.io/cheerpj3-saxonhe12-fiddle/?code=let+%24fact-base+%3A%3D+function%28%24n+as+xs%3Ainteger%2C%0D%0A+++++++++++++++++++%24f1+as+function%28xs%3Ainteger%2C%0D%0A+++++++++++++++++++function%28*%29%29+as+xs%3Ainteger%0D%0A+++++++++++++++++++%29+as+xs%3Ainteger%0D%0A+++++++++++++%7Bif%28%24n+eq+0%29%0D%0A+++++++++++++++++then+1%0D%0A+++++++++++++++++else+%24n+*+%24f1%28%24n+-1%2C+%24f1%29%0D%0A%0D%0A++++++++++++++%7D%2C%0D%0A++++%24Fact+%3A%3D+function%28%24n+as+xs%3Ainteger%29+as+xs%3Ainteger%0D%0A++++++++++++%7B%0D%0A++++++++++++++++%24fact-base%28%24n%2C+%24fact-base%29%0D%0A++++++++++++%7D%0D%0A%0D%0A+++return%0D%0A+++++++++++%24Fact%285%29&input=&code-type=XPath&input-type=None>.
>

Received on Sunday, 30 March 2025 23:40:58 UTC