如何在序言中编写一个包含 n 个元素的列表
How to program a list in prolog that has n number of elements
我正在尝试用 prolog 编写一个程序,returns 只有当列表首先只包含字母 {a,b,c} 并且它必须具有完全相同的数量时才为真a、b 和 c。此外,它必须按此顺序 [a,b,c]。
示例:[] 为真,[a,a,b,b,c,c] 为真,[a,a,a,b,b,b,c,c,c] 也为真,但 [b,b ,c,c,a,a] 为假,[b] 为假,[a,b] 为假,[a,c] 为假且 [a,a,b,b,b,c,c,c ]也是假的。
这是我尝试做的,但问题是对于每次迭代,第一个 c 没有放在最后:
langage11([]).
langage11(L) :-
langage11(L, []).
langage11([a, b, c | A], A).
langage11([a|A], B) :-
langage11(A, C),
C = [b, c|B].
希望有人能帮助我。
abc_list(ABCs) :-
length(ABCs, Len),
% The length is a multiple of 3
divmod(Len, 3, AmountOfEachChar, 0),
% The character-specific lengths are each a third of the total length
length(As, AmountOfEachChar),
same_length(As, Bs),
same_length(As, Cs),
% Contents of the character lists
maplist(=(a), As),
maplist(=(b), Bs),
maplist(=(c), Cs),
% Join the 3 lists
append([As, Bs, Cs], ABCs).
swi-prolog 中的结果:
?- findnsols(5, ABCs, abc_list(ABCs), Lst).
Lst = [[],[a,b,c],[a,a,b,b,c,c],[a,a,a,b,b,b,c,c,c],[a,a,a,a,b,b,b,b,c,c,c,c]]
我正在尝试用 prolog 编写一个程序,returns 只有当列表首先只包含字母 {a,b,c} 并且它必须具有完全相同的数量时才为真a、b 和 c。此外,它必须按此顺序 [a,b,c]。 示例:[] 为真,[a,a,b,b,c,c] 为真,[a,a,a,b,b,b,c,c,c] 也为真,但 [b,b ,c,c,a,a] 为假,[b] 为假,[a,b] 为假,[a,c] 为假且 [a,a,b,b,b,c,c,c ]也是假的。
这是我尝试做的,但问题是对于每次迭代,第一个 c 没有放在最后:
langage11([]).
langage11(L) :-
langage11(L, []).
langage11([a, b, c | A], A).
langage11([a|A], B) :-
langage11(A, C),
C = [b, c|B].
希望有人能帮助我。
abc_list(ABCs) :-
length(ABCs, Len),
% The length is a multiple of 3
divmod(Len, 3, AmountOfEachChar, 0),
% The character-specific lengths are each a third of the total length
length(As, AmountOfEachChar),
same_length(As, Bs),
same_length(As, Cs),
% Contents of the character lists
maplist(=(a), As),
maplist(=(b), Bs),
maplist(=(c), Cs),
% Join the 3 lists
append([As, Bs, Cs], ABCs).
swi-prolog 中的结果:
?- findnsols(5, ABCs, abc_list(ABCs), Lst).
Lst = [[],[a,b,c],[a,a,b,b,c,c],[a,a,a,b,b,b,c,c,c],[a,a,a,a,b,b,b,b,c,c,c,c]]