问题标签 [failure-slice]

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 回答
2232 浏览

prolog - Prolog Peano 算术中的 Stackoverflow

我正在写一些 Peano 算术来更好地学习 Prolog。以下是我提出的代码,它似乎与我在网上其他地方看到的相同:

但是,如果我尝试进行简单的查询,例如 的除数对0,我会遇到问题:

这真的让我很烦恼,至于它怎么能一直到Y = 3,但不是Y = 4呢?

0 投票
3 回答
214 浏览

prolog - Prolog 终止域:我如何知道哪些问题会返回有限数量的答案?

假设我有一个序言程序来连接这样的列表:

我怎么知道哪些问题会返回有限数量的答案?例如,询问

将返回有限解集:

在提出问题的同时

将返回一组无限的解决方案:

依此类推(基本上,每个可能的列表都以 [1,2,3] 结尾。

那么我如何推断逻辑程序上哪些问题会结束呢?

0 投票
3 回答
5527 浏览

list - Prolog从尾部找到列表中的最大整数

我需要从列表的头部或尾部找到列表中的最大整数。我已经编写了一个可以从头部找到最大的程序,现在我需要一些帮助才能从尾部开始。

这是我到目前为止所拥有的:

请记住,这会从头部找到最大的整数,我需要它从尾部开始工作。谢谢您的帮助。

0 投票
2 回答
2900 浏览

integer - 生成整数并测试是否为整数的谓词

我正在尝试制作一个 Prolog 谓词,它可以让我测试给定值是否是大于 0 的整数,并在给定变量的情况下给我一个有效的整数。现在看起来像这样:

这将生成所有整数,但是当用作 intgr(8) 时,它会发现它是有效的,然后永远循环。关于如何解决这个问题的任何想法?

0 投票
2 回答
943 浏览

prolog - Prolog - 规则是正确的,但没有按照预期的方式输出?

线索

四位客人(Mustard 上校、Plum 教授、Scarlett 小姐、Green 女士)参加了在 Boddy 先生家中的晚宴。突然,灯灭了!当他们回来时,博迪先生死在桌子中间。每个人都是嫌疑人。经过进一步检查,以下事实浮出水面:

  • 博迪先生与格林女士有染。
  • 普拉姆教授与格林女士结婚。
  • 博迪先生非常富有。
  • 芥末上校非常贪婪。
  • 斯嘉丽小姐也和博迪先生有染。

谋杀有两种可能的动机:

  • 仇恨:如果另一个人与他/她的配偶有染,那么这个人就会讨厌另一个人。
  • 贪婪:如果有人贪婪而不富有,而受害者是富有的,他们愿意杀人。

A 部分:在你的 Prolog 程序中写下上述事实和规则。使用以下人员名称:colMustard、profPlum、missScarlet、msGreen、mrBoddy。小心你如何编码(或不编码)对称关系,比如婚姻——你不想要无限循环!married(X,Y) :- married(Y,X) % INFINITE LOOP

B 部分:写一个谓词,suspect/2,确定嫌疑人可能是谁,即谁有动机。

C 部分:将一个事实添加到您的数据库中,这将导致存在一个独特的嫌疑人。在您的源注释中清楚地指出这一行,以便可以将其删除/添加以进行评分。


每当我输入

我明白了

我迷路了

这是我的来源。

0 投票
3 回答
176 浏览

prolog - Prolog:对列表进行排序时遇到问题

我有一个这样的列表,我正在尝试排序:

我想重新排列它,以便它按降序排列:

我有一个谓词提取给定数字的列表:

所以给定一个数字,它应该给我一个包含这些数字的元素列表,并有一个没有这些元素的提取列表。

然后我把它通过排序。我有一个最高数字,它应该从 0 循环到那个数字,提取那个数字的任何元素。直到我有排序列表。

但是,当我尝试这个时,它只是经历了一个无限循环。有人可以帮助我并告诉我哪里出错了吗?谢谢,

0 投票
3 回答
216 浏览

prolog - prolog中列表中一对的最小元素很慢

我在 GNU Prolog 中有这段代码,但我不知道为什么 50 元素配对列表很慢:

以下查询需要 10 秒:

我能做些什么来快速找到这个最小值?

0 投票
3 回答
2211 浏览

parsing - Parsing an expression in Prolog and returning an abstract syntax

I have to write parse(Tkns, T) that takes in a mathematical expression in the form of a list of tokens and finds T, and return a statement representing the abstract syntax, respecting order of operations and associativity.

For example,

I've attempted to implement + and * as follows

Which finds the correct answer, but also returns answers that do not follow associativity or order of operations.

ex)

also returns

and

returns the equivalent of 1+2+3 and 1+(2+3) when it should only return the former.

Is there a way I can get this to work?

Edit: more info: I only need to implement +,-,*,/,negate (-1, -2, etc.) and all numbers are integers. A hint was given that the code will be structured similarly to the grammer

Only with negate implemented as well.

Edit2: I found a grammar parser written in Prolog (http://www.cs.sunysb.edu/~warren/xsbbook/node10.html). Is there a way I could modify it to print a left hand derivation of a grammar ("print" in the sense that the Prolog interpreter will output "T=[the correct answer]")

0 投票
3 回答
1269 浏览

prolog - 在第一次解决斐波那契对后防止回溯

当th 斐波那契数为时,该术语fib(N,F)为真。FN

以下 Prolog 代码通常对我有用:

执行此查询时(在 SICStus Prolog 中),找到 N 的第一个(也是正确的)匹配项(相当立即):

当继续(通过输入“;”)查看是否有任何进一步的匹配(根据定义这是不可能的)时,总是需要大量的时间(与第一个匹配相比)才能始终回复“否”:

作为 Prolog 的新手,我尝试以!各种方式使用 Cut-Operator ( ),但我找不到阻止第一次匹配后搜索的方法。考虑到上述规则,它甚至可能吗?如果是,请告诉我如何:)

0 投票
2 回答
12917 浏览

prolog - Prolog“超出本地堆栈”错误

这是我的简单 Prolog 程序:

如果我键入以下查询

Prolog 将给出以下输出:

如果再输入一个分号,Prolog 会说:

我在这里做错了什么?