RE: Variables and lifetime of variables in XQuery

XQuery expressions are functional and not procedural. Especially the FOR
expression it a functional apply-iterator (think LISP, Haskell, ML, APL,
SQL and OQL) and not a procedural loop construct (as in C#, Java, C++
etc).

Thus every branch of a FOR is executed in parallel on the state before
the FOR and there is not way that one branch of a FOR has an inpact on
another branch. This property is very important for optimzability of the
expressions.

So in your example, the before state of the FOR/LET/WHERE is that $count
is 0. Since every branch of the FOR expression is basically saying LET
$count := 0 + 1, $count will always be only 1 in the return clauses.

So it is not the semantics of variables that is underdefined, but it is
the interaction/understanding of variables used in the context of
functional applies that may need better exposition in the document (note
that the formal semantics makes this behaviour clear, but not in a
layman understandable way).

Best regards
Michael

> -----Original Message-----
> From: Frank Wang [mailto:wang_fusheng@yahoo.com]
> Sent: Monday, January 07, 2002 16:04 PM
> To: XML-QL
> Subject: Variables and lifetime of variables in XQuery
> 
> In XQuery, we can define variables with LET and FOR
> clauses. But it seems to me the variable definition is
> not very clear, especially the lifetime of a variable.
> For example, I want to increment a variable "count"
> through a FOR loop:
> 
> LET $count : =  0
> FOR $book IN  document("bib.xml")/bib/book)
> LET $count : =  $count +1
> RETURN
> <countOfBooks>
>   {$count}
> </countOfBooks>
> 
> The expected result is:
>   <countOfBooks>1</countOfBooks>
>   <countOfBooks>2</countOfBooks>
>   <countOfBooks>3</countOfBooks>
>   <countOfBooks>4</countOfBooks>
>   <countOfBooks>5</countOfBooks>
>   <countOfBooks>6</countOfBooks>
>   <countOfBooks>7</countOfBooks>
>   <countOfBooks>8</countOfBooks>
> 
> But the returned result is (With QuiP from
> SoftwareAG):
> 
>   <countOfBooks>1</countOfBooks>
>   <countOfBooks>1</countOfBooks>
>   <countOfBooks>1</countOfBooks>
>   <countOfBooks>1</countOfBooks>
>   <countOfBooks>1</countOfBooks>
>   <countOfBooks>1</countOfBooks>
>   <countOfBooks>1</countOfBooks>
>   <countOfBooks>1</countOfBooks>
> 
> 
> So the lifetime of variables bound by LET In a FOR
> loop is the single loop.  With each new iteration, the
> count will be reinitialized. Is that correct?
> 
> Fusheng Wang
> Department of Computer Science
> University of California, Los Angeles
> Los Angeles, CA 90095
> 
> 
> __________________________________________________
> Do You Yahoo!?
> Send FREE video emails in Yahoo! Mail!
> http://promo.yahoo.com/videomail/

Received on Monday, 7 January 2002 19:24:58 UTC