Surprising function behavior

Tuesday, 30 November 2010 · 2 messages
MB
Mark Boon30 Nov 2010 03:36 #
I bumped into some surprising function behavior. I have the following query:
declare namespace math = "java:java.lang.Math";
declare function local:choose-phrase ( $phrase as xs:string ) as xs:string
			{
				let $prefix := local:choose(
					("Absolutely, ", "It's likely that ", "The odds are good that ",
"There's a fairly good chance that ", "There's a light chance that ",
"It's uncertain that ", "Chances are not good that ", "It's doubtful
that ", "It's unlikely that ", "There's no way that ")
				)
				return fn:concat($prefix,$phrase)
			};
declare function local:choose ( $arg as xs:string* ) as xs:string
			{
				let $count := fn:count($arg)
				let $random := math:floor(math:random() * $count) + 1
				return $arg[xs:integer($random)]
			};
local:choose(
						(
							"Phrase 1",
							"Phrase 2"
						)
					)
When I execute this (say in the GUI tool) it's supposed print either "Phrase 1" or "Phrase 2". Instead it prints a random choice from the function 'choose-phrase' as if it was called with an empty string as argument, which is very strange. When I remove the 'choose-phrase' function altogether, the query behaves correctly. If I then paste the 'choose-phrase' after the 'choose' function, it also behaves correctly. But declaring 'choose-phrase' before 'choose' gives me an unexpected result. The only thing I can think of is that 'choose-phrase' calls 'choose' which is declared later. I don't know if that's actually allowed. But even so, it should give an error instead of seemingly doing something random.
CG
Christian Grün30 Nov 2010 18:31 #
Hi Mark, I've tried your query, and it returns the correct result on my machine ("Phrase1" or "Phrase 2"). If you are working with version 6.1, please have a look at the latest version as well. The link to version 6.3.2 can now be found at: http://basex.org/download The next official version (along with all OS flavors and an overall updated homepage) will definitely be released in January '11. Christian
Quoted text · Mark Boon · 41 lines
On Tue, Nov 30, 2010 at 2:39 AM, Mark Boon <mboon at avatar-reality.com> wrote: > I bumped into some surprising function behavior. I have the following query: > > declare namespace math = "java:java.lang.Math"; > declare function local:choose-phrase ( $phrase as xs:string ) as xs:string > { > let $prefix := local:choose( > ("Absolutely, ", "It's likely that ", "The odds are good that ", > "There's a fairly good chance that ", "There's a light chance that ", > "It's uncertain that ", "Chances are not good that ", "It's doubtful > that ", "It's unlikely that ", "There's no way that ") > ) > return fn:concat($prefix,$phrase) > }; > declare function local:choose ( $arg as xs:string* ) as xs:string > { > let $count := fn:count($arg) > let $random := math:floor(math:random() * $count) + 1 > return $arg[xs:integer($random)] > }; > local:choose( > ( > "Phrase 1", > "Phrase 2" > ) > ) > > When I execute this (say in the GUI tool) it's supposed print either > "Phrase 1" or "Phrase 2". Instead it prints a random choice from the > function 'choose-phrase' as if it was called with an empty string as > argument, which is very strange. > > When I remove the 'choose-phrase' function altogether, the query > behaves correctly. If I then paste the 'choose-phrase' after the > 'choose' function, it also behaves correctly. But declaring > 'choose-phrase' before 'choose' gives me an unexpected result. > > The only thing I can think of is that 'choose-phrase' calls 'choose' > which is declared later. I don't know if that's actually allowed. But > even so, it should give an error instead of seemingly doing something > random.

⚡Generated with XQuery