|
[XQuery Talk Mailing List Archive Home] [By Date] [By Thread] [By Subject] [By Author] [Recent Entries] [Reply To This Message] Global variables in XQueryMichael Kay mhk at mhk.me.ukWed Oct 4 17:48:47 PDT 2006
XQuery is a functional language, and like other pure functional languages, it has no assignment statement. Your attempt to write an assignment statement is simply creating a local variable with the same name as the global variable (something that some of us tried to ban because we knew people would make this mistake). It's a common fallacy among programmers to think that variables are so called because they are mutable. In fact the term comes from mathematics where an expression such as X = X+1 would be considered rather strange (or at any rate, false). XQuery is an expression language, and the term "variable" simply means an input to the expression that can have different values on different occasions, just as "width" and "depth" are variables in the formula area=width*depth. I'm not sure what your example is trying to do. It's quite hard to reverse-engineer a declarative algorithm from a procedural one, especially an incorrect procedural one. It's usually easier to start again from the statement of requirements. Michael Kay http://www.saxonica.com/ > -----Original Message----- > From: http://x-query.com/mailman/listinfo/talk > [mailto:http://x-query.com/mailman/listinfo/talk] On Behalf Of Massimo Franceschet > Sent: 04 October 2006 16:25 > To: http://x-query.com/mailman/listinfo/talk > Subject: Global variables in XQuery > > Hi, > > I am confused about the behaviour of (what are called) global > variables in XQuery. In paricular, I am not able to modify > the value of a global variable inside a user-defined > function. Here is an example: > > declare namespace my = 'my:stuff'; > declare variable $my:global as xs:integer := 1; > > declare function my:fact($n as xs:integer) as xs:integer { > let $my:global := $my:global + 1 > return if ($n < 2) > then 1 > else $n * my:fact($n - 1) > }; > > let $x := my:fact(10) > return $x div $my:global > > When I run this query the global variable $my:global is not > modified by the function my:fact and retains its original > value (1). The result of the query is always 10! (instead of 10!/11). > > Massimo > > > _______________________________________________ > http://x-query.com/mailman/listinfo/talk > http://x-query.com/mailman/listinfo/talk
|
Purchase Stylus Studio Online Today!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|






