clojure - 为什么我的 clojure.core.logic 非成员函数返回两个值?

我正在尝试在 clojure.core.logic 中实现与 membero 相反的东西,但它返回两个值而不是一个值。否则,它工作正常(当值在列表中时不返回任何内容,而在列表中时不返回任何内容)。

(defne nonmembero
  "A relation where l is a collection, such that l does not contain x"
  [x l]
  ([_ ()])
  ([_ [head]]
     (!= x head))
  ([_ [head . tail]]
     (!= x head)
     (nonmembero x tail)))

运行示例:

user> (run* [x] (nonmembero 1 [2 3 4 5]))
(_0 _0)
user> (run* [x] (nonmembero 1 [2 3 1 4 5]))
()

最佳答案

您不需要第二个模式,即 [_ [head]。这会在 core.logic 引擎的搜索空间中产生一个新分支,从而导致输出 2。最后一个模式即 [head . tail] 足以处理列表中只有一个元素的情况。现在您的解决方案变为:

(defne nonmembero
  "A relation where l is a collection, such that l does not contain x"
  [x l]
  ([_ ()])
  ([_ [head . tail]]
     (!= x head)
     (nonmembero x tail)))

https://stackoverflow.com/questions/16370327/

相关文章:

Django - Python help_text 作为可点击的图标

avr - 使用USBASP编程器进行SPI通信

php - scandir 并返回匹配的文件

windows-installer - MSI 中的 "installation script"是什

python - 制作包含负值的对数标度彩色图

c - 在 C 中实现 hmac sha1

c# - 页脚按钮的 Gridview colspan

apache - 将模块加载到 Apache

r - 分位数回归的置信区间是多少?以及如何找到默认以外的内容?

versioning - 执行包装器库的 semver(语义版本控制)的最佳做法是什么?