How to count number of element occurrences in a list in Prolog

前端 未结 2 791
既然无缘
既然无缘 2020-11-30 15:56

i m new in prolog that s why may be the question is easy for you but i couldnt find the answer. Can someone please help me.

I just want

a count function s.t

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-30 16:30

    co(X,L) :- co(X,[],L).
    
    co([],A,A).
    co([X|Xs], A, L) :- p(X-Z,A,R), !, Z1 is Z+1, co(Xs, [X-Z1|R], L). 
    co([X|Xs], A, L) :- co(Xs, [X-1|A], L). 
    
    p(X-Y,[X-Y|R],R):- !.
    p(X,[H|Y], [H|Z]) :- p(X,Y,Z).
    

    I did not use very meaningful names on purpose. Try to understand what each one of the predicates does.

提交回复
热议问题