- に出現回数を数えます。
- 等しい要素の数を数えます。
イム近いので、私を助けてください:Prologは、私は、各要素のために、2リストを比較したい2つのリスト
%list vs list%
count2([],[],0).
count2([H1|T1],[H2|T2],S):-
count(H1,[H2|T2],N),
count2(T1,[H2|T2],M),
S is N+M.
%1 element vs 1 list%
count(_, [], 0).
count(X, [X | T], N) :-
!, count(X, T, N1),
N is N1 + 1.
count(X, [_ | T], N) :-
count(X, T, N).
A test:
1 ?- count2([2],[1,2,3],S).
false.
トレース:
2 ?- count2([2],[1,2,3],S).
Redo: (5) read_history(h, '!h', [trace, end_of_file], '~! ?- ', _G154, _G155) ? creep
Correct to: "count2([2],[1,2,3],S)"?
Please answer 'y' or 'n'? yes
Call: (7) count2([2], [1, 2, 3], _G306) ? creep
Call: (8) count(2, [1, 2, 3], _G631) ? creep
Call: (9) count(2, [2, 3], _G631) ? creep
Call: (10) count(2, [3], _G631) ? creep
Call: (11) count(2, [], _G631) ? creep
Exit: (11) count(2, [], 0) ? creep
Exit: (10) count(2, [3], 0) ? creep
Call: (10) _G632 is 0+1 ? creep
Exit: (10) 1 is 0+1 ? creep
Exit: (9) count(2, [2, 3], 1) ? creep
Exit: (8) count(2, [1, 2, 3], 1) ? creep
Call: (8) count2([], [1, 2, 3], _G637) ? creep
Fail: (8) count2([], [1, 2, 3], _G637) ? creep
Redo: (11) count(2, [], _G631) ? creep
Fail: (11) count(2, [], _G631) ? creep
Fail: (10) count(2, [3], _G631) ? creep
Fail: (9) count(2, [2, 3], _G631) ? creep
Fail: (8) count(2, [1, 2, 3], _G631) ? creep
Fail: (7) count2([2], [1, 2, 3], _G306) ? creep
false.
(溶液を返しますが、私は再帰に問題がある)
要求された出力# 1:
?- count2([2],[1,2,3],S).
S = 1.
(2リスト内の1時間です)。
要求された出力#2:
?- count2([1,2],[1,2,3],S).
S = 2
(1リスト内の1時間です)。 (2リスト内の1時間です)。 合計= 2の等しい要素。
何が効いていないのですか?あなたが得た出力と要求された出力を含むサンプルクエリを提供してください... –
Ok編集、一瞬 –
リストについての前提は何ですか?クエリ 'COUNT2([1,2]、[3,2,1]、S)は.'に何をもたらすべきですか? – lurker