[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: Sat, 13 Oct 2012 12:47:25 -0700 |
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.
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
Re: [xsl] XPath 3.0: Is it possible, Dimitre Novatchev | Thread | Re: [xsl] XPath 3.0: Is it possible, Dimitre Novatchev |
Re: [xsl] XPath 3.0: Is it possible, Dimitre Novatchev | Date | Re: [xsl] XPath 3.0: Is it possible, Dimitre Novatchev |
Month |