No, there is no tail-recusion optimization right now. But you can pass the
has-value flag into the function rather than use xdmp:set():
xquery version "1.0-ml";
declare function local:is-good-value($n) {
$n lt 500 and $n mod 11 eq 0
};
declare function local:f-rec($n, $done) {
let $val := if (local:is-good-value($n)) then $n else ()
let $done := exists($val)
return
if ($done) then $val
else local:f-rec($n - 1, $done)
};
local:f-rec(1000, false())
Also, I did a quick test on a pretty current linux machine and find the
recursive call overhead is only about 6 microseconds which isn't too bad.
Damon
________________________________
From: [email protected]
[[email protected]] On Behalf Of Alexander Boyd
[[email protected]]
Sent: Wednesday, March 09, 2011 1:42 PM
To: [email protected]
Subject: [MarkLogic Dev General] Does MarkLogic have tail recursion?
I’ve been searching around on Google for an answer to this, and I haven’t yet
found one: does MarkLogic support tail recursion optimization? I have a FLWOR
that calls a function repeatedly for each item in a list. If that function
returns something other than the empty list, it uses xdmp:set to set $has-value
to fn:true(). Each subsequent iteration checks to see if this variable is true,
and if it is, it just returns the empty list instead of actually calling the
function on that item. Since, in my case, the item in the list that causes the
function to return a value tends to be toward the beginning, this avoids the
performance overhead of calling the function for every item in the list.
However, I don’t particularly like that the use of xdmp:set prevents my code
from being purely-functional, so I’d like to replace this with tail recursion
if I can.
NOTICE: This email message is for the sole use of the intended recipient(s) and
may contain confidential and privileged information. Any unauthorized review,
use, disclosure or distribution is prohibited. If you are not the intended
recipient, please contact the sender by reply email and destroy all copies of
the original message.
_______________________________________________
General mailing list
[email protected]
http://developer.marklogic.com/mailman/listinfo/general