问题标签 [clojure-core.logic]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
181 浏览

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

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

示例运行:

0 投票
2 回答
233 浏览

clojure - 理解 core.logic !=

我希望以下表达式返回许多结果,每个结果都由两个 cons 单元格组成,其中两个 cons 单元格不等价。但是,它返回 0 个结果。为什么我没有结果?

预期结果示例:

  • [(1 . 4) (2 . 5)]
  • [(1 . 4) (1 . 5)]
  • [(2 . 4) (2 . 5)]

我不希望它返回像[(1 . 4) (1 . 4)]每个缺点中的两个点都相等的结果。

如果我删除该(!= c1 c2)部分,我会得到 16 个结果,包括两个缺点相同的结果。

如果替换为,我会得到预期的结果(!= c1 c2)

它应该做同样的事情,但明确检查两个单元格。

0 投票
1 回答
469 浏览

clojure - Clojure.logic 与 The Reasoned Schemer 的区别

我一直在使用 Clojure.logic 完成The Reasoned Schemer (TRS),并注意此处记录的差异。我到达第 3 章的第 24 帧,TRS 报告说

应该产生

现在,我实现了`lolo as

这会产生以下奇怪的结果:

这基本上意味着我的 lolo 正在产生泄漏出新变量的解决方案。如果我继续前进,试图看到一个模式,我会得到

但我在雾中看不清楚,我希望能对此有所了解。这是我lolo的一个错误吗?它是 clojure.logic 中的错误吗?TRS 中的求解器和 clojure.logic 中的求解器之间存在合理的区别吗?我如何解释或使用结果?我如何在心理上预测 clojure.logic 的结果?

0 投票
2 回答
473 浏览

clojure - 使用集合时的core.logic stackoverflow

clojure.core.logic 似乎在步行集上有问题。最小的失败示例:

(run* [q] (== q #{}))

生产

java.lang.StackOverflowError at clojure.core.logic.Substitutions.walk(logic.clj:344) at clojure.core.logic$walk_STAR_$fn_ 2633.invoke(logic.clj:216) at clojure.core.logic$eval2838 $fn _2839.invoke(logic.clj:956) at clojure.core.logic.protocols$eval1389$fn_ 1390$G _1380__1397.invoke(protocols.clj:55) at clojure.core.logic$walk_STAR_.invoke(logic. clj:214) at clojure.core.logic$walk_STAR_$fn_ 2633.invoke(logic.clj:218) at clojure.core.logic$eval2838$fn _2839.invoke(logic.clj:956) at clojure.core.logic .protocols$eval1389$fn_ 1390$G _1380__1397.invoke(protocols.clj:55) at clojure.core.logic$walk_STAR_.invoke(logic.clj:214) at clojure.core.logic$walk_STAR_$fn_2633.invoke(logic.clj:218) at clojure.core.logic$eval2838$fn _2839.invoke(logic.clj:956) at clojure.core.logic.protocols$eval1389$fn_ 1390$G _1380__1397.invoke(protocols .clj:55) 在 clojure.core.logic$walk_STAR_.invoke(logic.clj:214) 在 clojure.core.logic$walk_STAR_$fn_2633.invoke (logic.clj:218) 在 clojure.core.logic$eval2838 $fn _2839.invoke(logic.clj:956) at clojure.core.logic.protocols$eval1389$fn_ 1390$G _1380__1397.invoke(protocols.clj:55)

为什么这会生成 Stackoverflow?与空向量/列表/地图/其他类型统一按预期工作。

0 投票
1 回答
255 浏览

clojure - 哈希映射的成员?

我想知道是否有其他人曾经需要并因此编码了一个谓词,例如membero但用于哈希映射。当然,我可以(seq)在哈希映射上使用,但如果它已经是 LVar,它将无法工作。

如果我们调用它keyvalo,它的工作方式如下:

它可以定义为

但是我很难尝试编写 a 代码seqo,这对于(seqo [[:foo 3]] {:foo 3}).

谢谢!

0 投票
1 回答
129 浏览

clojure - Why (= (run 1 [q] (membero 'cat q)) ['(cat . _.0)]) is false?

I'm doing clojure/core.logic koans and stuck on this one:

Running (run 1 [q] (membero 'cat q)) in the REPL said me that the answer is ((cat . _.0)). I'm not quite sure what the dot in the middle means, but anyway, sticking '(cat . _.0) instead of the __ placeholder in the original koan doesn't help (assertion still fails). Could you please point me to the right direction? And also explain what the dot between cat and _.0 means? My guess is that it means what follows (i.e. _.0) is a tail of any length, but I'm not 100% sure. === UPDATE

amalloy pointed me to the right direction (thank you, sir!). lcons does the trick:

And a bit of REPL:

The dot is used to represent, well, dotted lists. According to Wikipedia, a dotted list is a kind of improper lists, where another kind is a circular list. So, in the REPL session above, the first list was a dotted list with two elements ('cat and '_.0), whereas the second list was a proper list with three elements ('cat', '. and '._0). Their string representation are the same, but nevertheless they are different.

0 投票
1 回答
490 浏览

clojure - clojure core.logic 计数集合中的元素

我尝试在 core.logic 中做这样的事情

这适用于整数就好了

但现在我正在尝试使用 core.logic 来解决一些问题,但它变得一团糟

但是问题来了, abc 没有作为值传递给函数。它们作为变量传递。使用三个变量 count-different-elements-in-list 总是返回 3 并且 core.logic 找不到解决方案(空列表)。

但我正在寻找这个结果。

0 投票
2 回答
235 浏览

prolog - 建模约束逻辑程序(用于分析)

面向对象的程序可以用不同的模型建模,例如自动机、过程代数、Petri 网或 UML。其中一些模型可用于执行各种分析,以发现性能或设计中的问题。

我正在研究逻辑编程,想知道 CLP 是否有这种模型?您如何分析 CLP 程序?

0 投票
3 回答
242 浏览

clojure - 如何处理 core.logic 中的目标列表

我确定这一定是一个愚蠢的问题,但我该如何处理 core.logic 中的目标列表?

将抛出异常,因为run*不需要列表。我觉得我需要做(apply all (f)),但all它是一个宏,所以这不起作用。

解决方案是什么?我觉得我在这里遗漏了一些微不足道的东西......

阿德里安。

0 投票
1 回答
195 浏览

clojure - 在 clojure.core.logic 中编码“三分之二”

我需要编写一些 core.logic 代码来检查三个目标中的两个是否成功。

我知道我可以这样写:

...但这很麻烦,我实际上需要针对“N out of M”案例概括我的代码,这很难概括。有人可以指出解决此类问题的正确方法吗?是否有可用的功能来简化这一点?

谢谢!