[xquery-talk] a conceptual clarification

Amitabh Ojha amitabhojha at rediffmail.com
Fri Mar 4 11:24:04 PST 2005


Dear Sir,

It has been a very useful exchange of  messages/ replies from experts on this theme. However, no discussion has taken place as yet on  one point which I had flagged earlier.  I elaborate it here.  

It is evident now  that  it is a bad practice to have two let statements, one after the other, define a variable  with the same name,  as  is  cited  in the following example  (even if it is not illegal as per XQuery Specs)

let $x := 


let $x := 



Following from this, will it be correct to assume that  the appearance of a let statement after a for statement be also treated as equally undesirable and error-prone programming technique in XQuery (because in course of  iteration,  a let statement is then forced to rebind  the same variable name say  $x several times, probably forcing it to bind to a new value each time).  I cite two examples here. In case of  my:foo () the result is same as one would expect in Java i.e  1, 2, 3, 4, 5. But in case of  my:bar () we get 101, 102, 103, 104, 105 – this is not what a Java programmer will expect (although in XQuery context one knows why it is so).

declare function my:foo ()
{
for $i in (1 to 5)
let $x := $i
return $x
};

declare function my:bar ()
{
let $x := 100
for $i in (1 to 5)
let $x :=  $x  +  $i
return $x
};

Regards.

Amitabh Ojha



  


On Thu, 03 Mar 2005 Per Bothner wrote :
>Ronald Bourret wrote:
>>The following is from query 2:
>>
>>   declare function my:square ($x)
>>   {
>>     let $x := $x * $x
>>     return $x
>>   };
>>
>>Just to be clear, this is legal because the $x in the parameter declaration is in a different namespace / symbol space / whatever you call it that the $x in the let statement?
>
>No - they're in the same name space.
>
>>But even if this is true, how does one decide which $x is returned by the return statement?
>
>This is "lexical scoping", as in many programming languages.
>The compiler "chooses" the *textually* "closest" definition.
>This is explained in any undergraduate "comparative programming
>languages" class or textbook.
>
>>Based on this, are the following functions legal or illegal?
>>
>>    declare function foo()
>>    {
>>       let $x := 1
>>       let $x := 2
>>       return $x
>>    }
>>
>>    declare function bar()
>>    {
>>       let $x := 2
>>       let $x := $x * $x
>>       return $x
>>    }
>
>Both are legal.  (Though I think allowing them is a Bad Idea.)
>
>In both cases the second "inner $x *hides* the first "outer" $x.
>But for the $x * $x "sees" the "outer $x.  Hence the result is 4.
>-- --Per Bothner
>per at bothner.com   http://per.bothner.com/
>_______________________________________________
>talk at xquery.com
>http://xquery.com/mailman/listinfo/talk
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://xquery.com/pipermail/talk/attachments/20050304/f1156b90/attachment.htm


More information about the talk mailing list