xsl-list
[Top] [All Lists]

RE: [xsl] Use pure XPath to test a sequence for being a valid Fibonacci sequence

2007-01-31 10:26:05
Not sure why you are using index-of (or some, for that matter). It looks to
me like

$fib[1] = 0 and $fib[2] = 1 and
  every $i in 2 to count($fib) satisfies
    $fib[$i] = $fib[$i - 1] + $fib[$i - 2]

Michael Kay
http://www.saxonica.com/ 

-----Original Message-----
From: Abel Braaksma [mailto:abel(_dot_)online(_at_)xs4all(_dot_)nl] 
Sent: 31 January 2007 17:13
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] Use pure XPath to test a sequence for being a 
valid Fibonacci sequence

Hi Xslt'ers,

I was wondering about the following: using some/every XPath 
expressions, is it possible to test values in a sequence 
against preceding values? 
The requirement pops up when you want to test pairs of 
values, where, say, the even indexed value must be greater 
than the odd indexed value, etc. I often have this 
requirement, and although it is resolvable with for-each 
and/or nested for-in-return, I was looking for a simpler solution.

Since many people understand the requirements of Fibonacci 
numbers, I think it is a good example use case and represents 
my actual use case pretty well (without the blur):

0, 1, 1, 2, 3, 5, 8, 13, 21...

Now, for each given Fibonacci number, it is correct if:

F(n) = F(n-1) + F(n-2)

For positions 1 and 2, if the value for F(n-1) and F(n-2) are zero.

This is what I could think of. It is not working, because 
index-of() function return two indices for the the number '1' 
(because it appears twice). It works if I omit these numbers, 
but then I cannot apply the 'every'. Does someone know of a 
way to use some/every instructions in a way that it satisfies 
the Fibonacci series above with 'every'? Perhaps it is simply 
not possible (or requires an awkward construction)?

some $i in $fib satisfies
    $fib[index-of($fib, $i) - 1] + $fib[index-of($fib, $i) - 2] = $i

$fib was defined as:
<xsl:variable name="fib" as="xs:integer*" select="(0, 1, 1, 
2, 3, 5, 8, 13, 21)" />

Cheers,
-- Abel Braaksma
   http://www.nuntia.nl




--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--