SWI Prolog 的排序程序 [英] Sorting program for SWI Prolog

查看:50
本文介绍了SWI Prolog 的排序程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这个程序在 SWI-PROLOG 中回答 False?

Why this program answers False in SWI-PROLOG?

sor(x, y):- sorted(y), perm(x, y).
sorted([]).
sorted([x, []]).
sorted([x, y, z]):- mi(x, y), sorted([y, z]).
perm([], []).
perm([x,y],[u,v]):- delete(u,[x,u],z), perm(z,v).
delete(x,[x,y],y].
delete(x, [y, z], [y, w]):- delete(x,z,w).
mi(0, x).
mi(s(x), s(y)):- mi(x, y).

对于查询?-

sor([s(s(s(s(s(0))))), s(s(s(s(s(s(0)))))), s(s(s(0))), s(s(0)), []], y).

这是对低效排序程序的 SWIProlog 的改编,在 Loyd 的 逻辑编程基础一书中用作示例(您可以在 this>,第 9 页)

This is an adaptation to SWIProlog of an inefficient sorting-program used as example in the book Foundations of Logic Programming, by Loyd (you can find the original SLOWSORT program example in this pdf, on page 9)

SWI Prolog 是标准的 Prolog,不是吗?

SWI Prolog is a standard Prolog, isn't it?

编辑

现在我尝试更正程序(稍微看一下 Prolog 中的列表语法)

Now I have tried to correct the program (looking a little to the lists syntax in Prolog)

sor(X, Y):- perm(X, Y), sorted(Y).
sorted([]).
sorted([X|[]]).
sorted([X|[Y|Z]]):- mi(X, Y), sorted([Y|Z]).
perm([], []).
perm([X|Y],[U|V]):- delete(U,[X|Y],Z), perm(Z, V).
delete(X,[X|Y],Y). 
delete(X, [Y|Z], [Y|W]):- delete(X, Z, W).
mi(0, X).
mi(s(X), s(Y)):- mi(X, Y).

并在

sor([s(s(s(s(s(0)))))|[ s(s(s(s(s(s(0))))))|[s(s(s(0)))|[ s(s(0))|[]]]]], Y).

好吧,Prolog 现在提供了成功,但它提供了这个替代

Well, Prolog now gives success, but it gives this substitution

Y = [s(s(0)), s(s(s(0))), s(s(s(s(s(0))))), s(s(s(s(s(s(...))))))]

而且我不明白 (...) 的含义:为什么不是 (0)?

and I don't understand the meaning of (...): Why not (0)?

编辑 2

我注意到在给出命令 swipl -s slowsort.pl 后我得到了这个错误信息

I notice that after giving the command swipl -s slowsort.pl I obtain this error message

Warning: /home/navigazione/Scrivania/slowsort.pl:3:
Singleton variables: [X]
Warning: /home/navigazione/Scrivania/slowsort.pl:9:
Singleton variables: [X]

好像是指程序的第3行和第9行,但我不明白是什么意思.

It seems to refer to 3th and 9th rows of the program, but I don't understand what it means.

推荐答案

太好了,你设法将它翻译成正确的 Prolog :)

Great, you managed to translate it to correct Prolog :)

你看到的是顶层试图通过省略一些东西来使事情变得可读(... 意味着那里有没有显示的东西).请参阅此问题和答案,了解您可以通过不同方式判断显示完整术语的顶层,而不是隐藏它的一部分.

What you see is the top level trying to make things readable by omitting stuff (the ... means there is stuff there that is not shown). See this question and answers for different ways you can tell the top level to show the complete term instead of hiding parts of it.

至于单例变量警告,它只是告诉您您有逻辑变量(在第 3 行和第 9 行),您在其语法范围内只提到了一次.您可以编写 _X 而不是 X 以明确表示您没有使用该范围内的变量值.

As for the singleton variable warnings, it just tells you that you have logical variables (on lines 3 and 9) that you have only mentioned once in their syntactical scope. You can write _X instead of X to make it explicit that you are not using the value of the variable in that scope.

这篇关于SWI Prolog 的排序程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆