Number of Prolog Instances

I am new to Prolog and trying to make some list programs
I want to do this:

?- count_occurrences([a,b,c,a,b,c,d], X).
X = [[d, 1], [c, 2], [b, 2], [a, 2]].

and this is my code. I know that it is not complete, but I'm trying:

count_occurrences([],[]).
count_occurrences([X|Y],A):-
   occurrences([X|Y],X,N).

occurrences([],_,0).    
occurrences([X|Y],X,N):- occurrences(Y,X,W), N is W + 1.
occurrences([X|Y],Z,N):- occurrences(Y,Z,N), X\=Z.

My code is wrong, so I need some hits or plz help ..

+2
source share
6 answers

Here is my solution using bagof/3and findall/3:

count_occurrences(List, Occ):-
    findall([X,L], (bagof(true,member(X,List),Xs), length(Xs,L)), Occ).

Example

?- count_occurrences([a,b,c,b,e,d,a,b,a], Occ).
Occ = [[a, 3], [b, 3], [c, 1], [d, 1], [e, 1]].

How it works

bagof(true,member(X,List),Xs)is executed for each individual element of the list Xwith Xs, which is a list with its length equal to the number of entries Xin List:

?- bagof(true,member(X,[a,b,c,b,e,d,a,b,a]),Xs).
X = a,
Xs = [true, true, true] ;
X = b,
Xs = [true, true, true] ;
X = c,
Xs = [true] ;
X = d,
Xs = [true] ;
X = e,
Xs = [true].

Outer findall/3collects the item Xand the length of the linked list Xsin the list that represents the solution.

I: CapelliC Boris.

Edit II: setof/3 findall/3, . setof/3 , , .

count_occurrences([],[]).
count_occurrences(List, Occ):-
    setof([X,L], Xs^(bagof(a,member(X,List),Xs), length(Xs,L)), Occ).
+1

, , . :

?- count_occurrences([a,X], D).

.

   X = a, D = [a-2] ;
   dif(X, a), D = [a-1,X-1].

: [a,a] a , , , D = [a-2]. X, a, a . , , X = b X = c - , .

, . - :

count_occurrences(Xs, D) :-
   ( ground(Xs) -> true ; throw(error(instantiation_error,_)) ),
   ... .

Prolog , . .

. , . @dasblinkenlight .

if-then-else. (;)/2

   ( If_0 -> Then_0 ; Else_0 )

.

   if_( If_1, Then_0, Else_0)

. - . If_0, . ( X = Y -> Then_0 ; Else_0 ), X Y , Then_0 Else_0. , ? , , Then_0.

if_( If_1, Then_0, Else_0). , , Then_0 Else_0. , .

count_occurrences(Xs, D) :-
   foldl(el_dict, Xs, [], D).

el_dict(K, [], [K-1]).
el_dict(K, [KV0|KVs0], [KV|KVs]) :-
    KV0 = K0-V0,
    if_( K = K0,
         ( KV = K-V1, V1 is V0+1, KVs0 = KVs ),
         ( KV = KV0, el_dict(K, KVs0, KVs ) ) ).

=(X, Y, R) :-
   equal_truth(X, Y, R).

: if_/3, equal_truth/3, foldl/4.

+3

SWI-Prolog, :

:- use_module(library(lambda)).

count_occurrences(L, R) :-
    foldl(\X^Y^Z^(member([X,N], Y)
             ->  N1 is N+1,
             select([X,N], Y, [X,N1], Z)
             ;   Z = [[X,1] | Y]),
          L, [], R).
+2

, , - , .

, [SomeAtom,Count] , , [SomeAtom,1] . :

increment([], E, [[E,1]]).
increment([[H,C]|T], H, [[H,CplusOne]|T]) :-
    CplusOne is C + 1.
increment([[H,C]|T], E, [[H,C]|R]) :-
    H \= E,
    increment(T, E, R).

, . , . - , .

count_occ :

count_occ([], []).
count_occ([H|T], R) :-
    count_occ(T, Temp),
    increment(Temp, H, R).

Prolog run-of-the-mill , , increment .

-

+1

. Prolog - , "" . , - . , , :

  • ( )

, ( ) -.

: msort(List, Sorted)

- , , , , List → Encoding. ( ):

list_to_rle([], []).
list_to_rle([X|Xs], RLE) :-
    list_to_rle_1(Xs, [[X, 1]], RLE).

list_to_rle_1([], RLE, RLE).
list_to_rle_1([X|Xs], [[Y, N]|Rest], RLE) :-
    (    dif(X, Y)
    ->   list_to_rle_1(Xs, [[X, 1],[Y, N]|Rest], RLE)
    ;    succ(N, N1),
         list_to_rle_1(Xs, [[X, N1]|Rest], RLE)
    ).

, :

?- msort([a,b,c,a,b,c,d], Sorted), list_to_rle(Sorted, RLE).
Sorted = [a, a, b, b, c, c, d],
RLE = [[d, 1], [c, 2], [b, 2], [a, 2]].

"", X-N, , [X, N]. , , . :

rle([], []).
rle([First|Rest],Encoded):- 
    rle_1(Rest, First, 1, Encoded).               

rle_1([], Last, N, [Last-N]).
rle_1([H|T], Prev, N, Encoded) :-
    (   dif(H, Prev) 
    ->  Encoded = [Prev-N|Rest],
        rle_1(T, H, 1, Rest)
    ;   succ(N, N1),
        rle_1(T, H, N1, Encoded)
    ).

?

  • 4

  • : compare .(a, .(1, [])) to -(a, 1)

  • ( Prolog)

:

?- msort([a,b,c,a,b,c,d], Sorted), rle(Sorted, RLE).
Sorted = [a, a, b, b, c, c, d],
RLE = [a-2, b-2, c-2, d-1].

The presented string length encoder is very clear in its definition, which, of course, has its pros and cons. See this answer for a more concise way to do this.

+1
source

clarification joel76 answer :

count_occurrences(L, R) :-
    foldl(\X^Y^Z^(select([X,N], Y, [X,N1], Z)
             ->  N1 is N+1
             ;   Z = [[X,1] | Y]),
          L, [], R).
+1
source

Source: https://habr.com/ru/post/1616560/


All Articles