在 SWI-Prolog 中填写列表
我正在尝试用数字 1,2,3,...,N 填充给定长度 N 的列表。
我认为这可以这样做:
create_list(N,L) :-
length(L,N),
forall(between(1,N,X), nth1(X,L,X)).
然而,这似乎不起作用。谁能说我做错了什么?
I am trying to fill a list of given length N with numbers 1,2,3,...,N.
I thought this could be done this way:
create_list(N,L) :-
length(L,N),
forall(between(1,N,X), nth1(X,L,X)).
However, this does not seem to work. Can anyone say what I am doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,使用clpfd!< /strong>
下面我将介绍
zs_ Between_and/3
,它(与我之前的答案相比)提供了更多功能。首先,让我们先定义一些辅助谓词!
让我们运行一些查询来获取
equidistant_stride/2
的图片:到目前为止,一切顺利...继续讨论实际的“填充列表”谓词
zs_ Between_and/3
:我必须承认,有点巴洛克...
让我们看看获得了哪些功能——与我的上一个答案!
想要更多吗?开始了!
First things first: Use clpfd!
In the following I present
zs_between_and/3
, which (in comparison to my previous answer) offers some more features.For a start, let's define some auxiliary predicates first!
Let's run a few queries to get a picture of
equidistant_stride/2
:So far, so good... moving on to the actual "fill list" predicate
zs_between_and/3
:A bit baroque, I must confess...
Let's see what features were gained---in comparison to my previous answer!
Want some more? Here we go!
我现在没有可用的 prolog 解释器,但是不会像...
反向可以通过使用例如 http://www.webeks.net/prolog/prolog-reverse-list-function.html
因此跟踪 isListTo(5, [1, 2, 3, 4 , 5])...
由于 PROLOG 不仅会评估真理,还会找到令人满意的解决方案,因此这应该可行。我知道这是一种与您正在尝试的方法截然不同的方法,如果您的问题专门涉及在 PROLOG 中进行循环,我深表歉意(如果是这种情况,也许重新标记问题?)。
I don't have a prolog interpreter available right now, but wouldn't something like...
reverse can be done by using e.g. http://www.webeks.net/prolog/prolog-reverse-list-function.html
So tracing isListTo(5, [1, 2, 3, 4, 5])...
Since PROLOG will not only evaluate truth, but find satisfying solutions, this should work. I know this is a vastly different approach from the one you are trying, and apologize if your question is specifically about doing loops in PROLOG (if that is the case, perhaps re-tag the question?).
这是使用 clpfd:
让我们使用它!首先,一些基本查询:
接下来,一些更一般的查询:
现在,让我们来一些更一般的查询:
我们得到什么答案最常见的查询?
编辑 2015-06-07
为了改进上述
zs_from_to/3
的实现,我们要做两件事:zs_from_to/3
。介绍元谓词
init0/3
和init1/3
:让我们看看
init0/3
和init1/3
在行动!好吧,我们该去哪里?考虑以下查询:
几乎完成!把它们放在一起,我们定义
zs_from_to/2
如下:最后,让我们看看确定性是否有所改进!
Here's a logically pure implementation of predicate
zs_from_to/3
using clpfd:Let's use it! First, some ground queries:
Next, some more general queries:
Now, let's have some even more general queries:
What answers do we get for the most general query?
Edit 2015-06-07
To improve on above implementation of
zs_from_to/3
, let's do two things:zs_from_to/3
on top of it.Introducing the meta-predicates
init0/3
andinit1/3
:Let's see
init0/3
andinit1/3
in action!Ok, where do we go from here? Consider the following query:
Almost done! Putting it together, we define
zs_from_to/2
like this:At last, let's see if determinism has improved!
如果我理解正确的话,内置谓词 numlist/3 就可以了。
http://www.swi-prolog.org/pldoc/man?predicate =numlist/3
If I understood correctly, the built-in predicate numlist/3 would do.
http://www.swi-prolog.org/pldoc/man?predicate=numlist/3