Bug in array:fold-left() and array:fold-right()

An error in the specification of array:fold-right() was reported on the xquery-talk mailing list this morning. I have logged it here:

https://www.w3.org/Bugs/Public/show_bug.cgi?id=30045

Although the report was against array:fold-right(), the same error is present in array:fold-left().

The normative rules for the functions say one thing, but all the examples, notes, and test-cases say otherwise. So although one could argue that the normative prose wins, it's very clear that this wasn't the WGs intent.

fold-left defines the effect as:

if (array:size($array) eq 0)
then [$zero]
else array:fold-left(array:tail($array), 
                     $function($zero, array:head($array)), 
                     $function )

but this would always deliver an array of length one, which is useless and clearly not intended. The correct code should be

if (array:size($array) eq 0)
then $zero
else array:fold-left(array:tail($array), 
                     $function($zero, array:head($array)), 
                     $function )

Given where we are in the process, I propose to commit this change now, and ask the WG to approve the fix at its next meeting. If you object to this (which you are entitled to do, given that we just voted to take the spec "as is" to PR) then please shout now.


Michael Kay
Saxonica

Received on Thursday, 12 January 2017 13:21:06 UTC