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

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: Sun, 14 Oct 2012 06:48:37 -0700

On Sun, Oct 14, 2012 at 1:03 AM, Michael Kay <mike@xxxxxxxxxxxx> wrote:
> 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?


It (using a named function) is simpler if one writes in XSLT or XQuery.

It is impossible, if one doesn't.


Cheers,
Dimitre




>
> 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.
>



-- 
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
Keywords