Prolog Count出现在2个列表中

时间:2017-07-02 14:01:30

标签: list count prolog recursive-backtracking

  • 我想比较每个元素的2个列表。
  • 计算相等元素的数量。

我很近,请帮助我:

array (
  0 => 
  array (
    'category_id' => '11',
    'image' => '718426050751.jpg',
    'parent_id' => '1',
    'top' => '0',
    'column' => '1',
    'sort_order' => '21',
    'status' => '1',
    'date_added' => '2015-07-30 14:06:42',
    'date_modified' => '2016-01-05 12:21:32',
    'language_id' => '1',
    'name' => 'Faucets',
    'description' => '',
    'meta_description' => '',
    'meta_keyword' => '',
    'store_id' => '1',
    'product_count' => '0',
    'page_count' => '0',
  ),
  1 => 
  array (
    'category_id' => '13',
    'image' => '4005176268779.jpg',
    'parent_id' => '11',
    'top' => '0',
    'column' => '1',
    'sort_order' => '2',
    'status' => '1',
    'date_added' => '2015-07-30 14:06:43',
    'date_modified' => '2016-01-07 14:10:53',
    'language_id' => '1',
    'name' => 'Sink Faucets',
    'description' => '',
    'meta_description' => '',
    'meta_keyword' => '',
    'store_id' => '1',
    'product_count' => '0',
    'page_count' => '0',
  ),
)

跟踪:

%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.

(返回解决方案,但我的递归有问题)

请求输出#1:

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.

(2是列表中的1次)。

请求输出#2:

?- count2([2],[1,2,3],S).
S = 1.

(1是列表中的1次)。 (2是列表中的1次)。 总计= 2个相等的元素。

2 个答案:

答案 0 :(得分:3)

一个更简单的解决方案是递归主列表的元素并检查每个元素是否是测试列表的成员:

count(_, [], 0).
count(Xs, [H|T], C) :-
    (   member(H, Xs)
    ->  C #= C1 + 1
    ;   C1 = C
    ),
    count(Xs, T, C1).

答案 1 :(得分:0)

答案:

count2([],[_|_],0).
count2([H1|T1],[H2|T2],S):-
    count(H1,[H2|T2],N),
    count2(T1,[H2|T2],M),
    S is N+M.


count(_, [], 0).
count(X, [X | T], N) :-
  !, count(X, T, N1),
  N is N1 + 1.
count(X, [_ | T], N) :-
  count(X, T, N).

Solved!