返回布尔值的递归函数,内部有一个 for 循环

发布于 2024-12-05 17:12:25 字数 467 浏览 0 评论 0原文

我的数据是一个二叉树,并且会检查每个子节点,如果找到我想要的数据则返回 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情场扛把子 2024-12-12 17:12:25

这是 XQuery 量化表达式 的情况。使用它,你的函数可以转换为

declare function local:test($id as xs:integer, $topic as xs:integer) as xs:boolean
{
  some $x in ...
  satisfies
    if (..) then
      fn:true()
    else
      local:test($x,$topic)
};

This is a case for an XQuery quantified expression. Using that, your function translates to

declare function local:test($id as xs:integer, $topic as xs:integer) as xs:boolean
{
  some $x in ...
  satisfies
    if (..) then
      fn:true()
    else
      local:test($x,$topic)
};
心的位置 2024-12-12 17:12:25

正如已经提到的,XQuery 是一种函数式语言。您不能只设置变量并返回它。您的查询可以重写为:

declare function local:test($id as xs:integer, $topic as xs:integer) as xs:boolean {
    exists(for $x in ...
           where (: here is condition expression on $x :)
           return $x)
};

如果 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:

declare function local:test($id as xs:integer, $topic as xs:integer) as xs:boolean {
    exists(for $x in ...
           where (: here is condition expression on $x :)
           return $x)
};

Function exists(Expr) returns true if the value of Expr is not the empty sequence; otherwise, the function returns false.

In this case exists returns true if there is $x which meets specified condition.

王权女流氓 2024-12-12 17:12:25

您无法更改 xquery 中变量的值。

你的整个功能不只是这样吗:

declare function local:test($topic as xs:integer) as xs:boolean {
     ... OR local:test($topic/...)
};

You cannot change the values of variables in xquery.

Is your whole function not just this:

declare function local:test($topic as xs:integer) as xs:boolean {
     ... OR local:test($topic/...)
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文