序言:介于/3之间,末尾有一个列表 [英] Prolog: between/3 with a list at the end
问题描述
我的问题因这个问题而出现 您能在纯序言中在/3之间写作吗? >
是否可以在/3和第三个参数之间建立一个列表,所以如果您问
between(2,6,X).
它来了
X=[2,3,4,5,6]
不喜欢
X=2
X=3
X=4
....
我不知道该如何工作(我的所有解决方案均无效.) 我是Prolog的初学者,所以我不知道.
对不起,英语不好.
感谢您的帮助:)
首先进入图书馆并获得一本好书,例如Sterling和Shapiro的序言的艺术".
两种方式:
?- findall(X, between(2, 6, X), Xs).
Xs = [2, 3, 4, 5, 6].
您还应该查看bagof/3
和setof/3
.
对于直接方法numlist/3
,请参见例如 SWI-Prolog的实现.没有参数检查,它可以归结为:
numlist(U, U, List) :- !,
List = [U].
numlist(L, U, [L|Ns]) :-
L2 is L+1,
numlist(L2, U, Ns).
有几种方法可以打破谓词的原状.
?- numlist(1,0,L).
不会终止.您需要先检查参数,然后再将其传递给此特定版本的numlist/3
:
must_be(integer, L),
must_be(integer, U),
L =< U
这些检查并入链接的SWI-Prolog实现的库谓词numlist/3
中.
my question comes up because of this question Can you write between/3 in pure prolog?
would it be possible to make between/3 and the third argument is a list so if you ask
between(2,6,X).
it comes
X=[2,3,4,5,6]
and not like
X=2
X=3
X=4
....
I can’t figure out how this must work (all my solutions don’t work..) I’m a Prolog beginner so I have no idea..
sorry for the bad English..
Thanks for your help :)
Start by going to the library and getting a good book, for example "The Art of Prolog" by Sterling and Shapiro.
Two ways:
?- findall(X, between(2, 6, X), Xs).
Xs = [2, 3, 4, 5, 6].
You should also take a look at bagof/3
and setof/3
.
For a direct way, numlist/3
, see for example the SWI-Prolog implementation. Without argument checking it comes down to:
numlist(U, U, List) :- !,
List = [U].
numlist(L, U, [L|Ns]) :-
L2 is L+1,
numlist(L2, U, Ns).
There are several ways to break the predicate as it stands.
?- numlist(1,0,L).
will not terminate. You need to either check the arguments before passing them to this particular version of numlist/3
:
must_be(integer, L),
must_be(integer, U),
L =< U
These checks are incorporated in the library predicate numlist/3
from the linked SWI-Prolog implementation.
这篇关于序言:介于/3之间,末尾有一个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!