[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Use pure XPath to test a sequence for being a vali

Subject: Re: Use pure XPath to test a sequence for being a valid Fibonacci sequence
From: Abel Braaksma <abel.online@xxxxxxxxx>
Date: Wed, 31 Jan 2007 22:36:25 +0100
Re:  Use pure XPath to test a sequence for being a vali
Abel Braaksma wrote:
Michael Kay wrote:

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

Aha! (erlebnis)

For completeness, I will add a slightly corrected version (I saw the light, thanks to Michael). The 'every' operator has lower precedence than 'and', requiring parenthesis (otherwise you will receive an error) and the '.. to ..' range should start with 3:


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

This returns true for example for the following Fibonacci sequence:
(0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025)



Here's another version, which eases the expression a bit by removing the special cases:


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

-- Abel

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2007 All Rights Reserved.