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

Re: 99 bottles of beer

Subject: Re: 99 bottles of beer
From: Abel Braaksma <abel.online@xxxxxxxxx>
Date: Mon, 05 Feb 2007 21:10:15 +0100
Re:  99 bottles of beer
Andrew Welch wrote:

Nice! However a couple of small problems:


2 bottles of beer on the wall.
2 bottles of beer.
Take one down, pass it around
1 bottles of beer on the wall.
     ^^^^^^^

1 bottle of beer on the wall.
1 bottle of beer.
Take one down, pass it around
0 bottle of beer on the wall.
      ^^^^^^^^

You just need to modify it slightly:

Thanks for the correction. But now we have duplicated logic (the cast + ends-with), which some consider bad programming practice. Here's an update that does not duplicate the logic, removes the nested for (better: it shows a way of shortcutting nested loops with the comma operator), corrects my error and is much less readable (which was not on purpose, btw).


for $i in reverse(1 to 99),
   $j in (1 to 3)

return concat
($i - xs:integer($j mod 3 = 0),
' bottle',
('s')[not($i * 3 - $j = (1 to 3))],
' of beer',
(' on the wall.',
'.&#10;Take one down, pass it around',
' on the wall.&#10;' )[$j]



(I like the idea of using the predicate instead of if...then...else)


As a tutorial it suits well for explaining why:

('s')[$i * 3 - $j != (1 to 3)]

yields exactly the same result as:

('s')[$i * 3 - $j = (1 to 3)]

which is quite counter-intuitive (IIRC, for a '!=' to return false, none from the left must be the same as any from the right; to be true, only one item needs to be unequal. This is not backed up by the example above, but I am sure I am overlooking something. Moreover, I found that (1 to 2) != 10 returns false, and (1,2) != 10 returns true.... I am really missing something here, this must be a faq somewhere :S ).


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

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-2011 All Rights Reserved.