返回布尔值的递归函数,内部有一个 for 循环
我的数据是一个二叉树,并且会检查每个子节点,如果找到我想要的数据则返回 true,如果没有,它会继续寻找它。 以某种方式,我想返回变量@exists或其他东西。好吧,任何人都可能有解决我的问题的方法。我在想类似的事情,但我无法让它发挥作用! (代码片段)
declare function local:test($id as xs:integer, $topic as xs:integer) as xs:boolean {
let $exists := fn:false()
for $x in ...
return
if .. then
set exists to fn:true()
else
set exists to exists OR local:test($x,$topic)
return @exists in some way
};
My data is a binary tree, and will check through every child, returning true if it finds the data i want, if not, it keeps looking for it.
In some way i want to return the variable @exists or something.. Well anyone might have a solution for my problem. I was thinking something like this but i couldn't get it to work! (code-snippet)
declare function local:test($id as xs:integer, $topic as xs:integer) as xs:boolean {
let $exists := fn:false()
for $x in ...
return
if .. then
set exists to fn:true()
else
set exists to exists OR local:test($x,$topic)
return @exists in some way
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是 XQuery 量化表达式 的情况。使用它,你的函数可以转换为
This is a case for an XQuery quantified expression. Using that, your function translates to
正如已经提到的,XQuery 是一种函数式语言。您不能只设置变量并返回它。您的查询可以重写为:
如果
Expr
的值不是空序列,则函数exists(Expr)
返回true
;否则,该函数返回false
。在这种情况下,如果存在满足指定条件的
$x
,则exists
返回true
。As it was already mentioned XQuery is a functional language. You can't just set variable and return it. Your query can be though rewritten like:
Function
exists(Expr)
returnstrue
if the value ofExpr
is not the empty sequence; otherwise, the function returnsfalse
.In this case
exists
returnstrue
if there is$x
which meets specified condition.您无法更改 xquery 中变量的值。
你的整个功能不只是这样吗:
You cannot change the values of variables in xquery.
Is your whole function not just this: