Home > Archive > Prolog > February 2007 > Counter of elements in prolog
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Counter of elements in prolog
|
|
| Sied@r 2007-01-26, 7:05 pm |
|
Hi All
I'm writing program to count all of repetition in list.
Looks like
"""""""""""""""""""
count([],A,0):-!.
count([A|Tail],A,Y) :-
count(Tail,A,Z),
Y is 1 + Z.
count([_|Tail],A,Y) :-
count(Tail,A,Z),
Y is Z.
and I'm asking count([2,2,3,1,3],A,Y).
I get only answer for first repetition
A=2
Y=2
What Can I do to count all repetition on the list ?
to get result like this ?:
result for all uniq numbers
A=2
Y=2
A=3
Y=2
A=1
Y=1
Greetins for any help :)
D.
| |
| Heuchi 2007-01-26, 10:02 pm |
| Hi!
> and I'm asking count([2,2,3,1,3],A,Y).
>
> I get only answer for first repetition
>
> A=2
> Y=2
I just tried it myself and I got a lot(!) more solutions. Have you tried to
ask the system for more solutions? SWI-Prolog for example comes up with the
first (A=2,Y=2) and waits for you to enter a key. Press ';' for more
solutions as long as there are others.
I'm getting (A,Y)
(2,2) (2,1) (2,1) (3,2) (3,1) (1,1) (3,1) (_G291,0)
So probably a lot more as you want it to produce...
Greetings,
J.
| |
| Sied@r 2007-01-26, 10:03 pm |
| Heuchi napisa=B3(a):
> Hi!
>=20
>=20
> I just tried it myself and I got a lot(!) more solutions. Have you trie=
d to=20
> ask the system for more solutions? SWI-Prolog for example comes up with=
the=20
> first (A=3D2,Y=3D2) and waits for you to enter a key. Press ';' for mor=
e=20
> solutions as long as there are others.
>=20
> I'm getting (A,Y)
> (2,2) (2,1) (2,1) (3,2) (3,1) (1,1) (3,1) (_G291,0)
>=20
> So probably a lot more as you want it to produce...
>=20
> Greetings,
>=20
> J.
>=20
>=20
thanks - yep I think this is much more that I need
is there posibility to filter to only uniq answers ?
D.
| |
|
| On Sat, 27 Jan 2007 00:43:22 +0100, "Sied@r"
<siedar_bez_spamu@ds.lub.pl> wrote:
>
>Hi All
>
>I'm writing program to count all of repetition in list.
>
What it exactly means "counting repetitions"?...
A.L.
| |
| Peter Van Weert 2007-01-30, 7:06 pm |
| Sied@r schreef:
[color=darkred]
> thanks - yep I think this is much more that I need
>
> is there posibility to filter to only uniq answers ?
>
> D.
Maybe one of the following programs is more what you need (for your
homework??)...
% longer
count([A|As], B, Y) :-
count(As, A, X, R),
(
Y is X+1,
B = A
;
count(R, B, Y)
).
count([], _, 0, []).
count([A|As], A, Y, R) :- !,
count(As, A, X, R),
Y is X+1.
count([B|As], A, Y, [B|R]) :-
count(As, A, Y, R).
/*
% shorter
count(L, A, Y) :-
sort(L, S),
member(A, S),
sublist('='(A), L, SL),
length(SL, Y).
*/
Cheers,
Peter
| |
| Thorsten Kiefer 2007-02-03, 7:05 pm |
| Sied@r wrote:
> count([],A,0):-!.
>
> count([A|Tail],A,Y) :-
> count(Tail,A,Z),
> Y is 1 + Z.
>
> count([_|Tail],A,Y) :-
> count(Tail,A,Z),
> Y is Z.
>
I didn't try it, but I think you are missing a cut !!
count([],A,0):-!. <<<<<< I guess that cut is unnecessary, but maybe a performance improvement
count([A|Tail],A,Y) :-
count(Tail,A,Z),
Y is 1 + Z,!. <<<<<<<<<<<here
count([_|Tail],A,Y) :-
count(Tail,A,Z),
Y is Z.
I guess the cut in your original version is just misplaced, right ?
| |
| Thorsten Kiefer 2007-02-03, 7:05 pm |
| Another approach :
unify([],[]).
unify([X|Xs],[X|Ys]) :- not(member(X,Xs)),unify(Xs,Ys),!.
unify([_|Xs],Ys) :- unify(Xs,Ys).
inner_count([],_,0):-!.
inner_count([X|Xs],X,C) :- inner_count(Xs,X,C2),C is C2+1,!.
inner_count([_|Xs],X,C) :- inner_count(Xs,X,C),!.
count(List,Member,Count):- unify(List,UnitList),!,member(X,UnitList
),inner_count(List,X,Count).
Regards
Thorsten
Sied@r wrote:
>
> Hi All
>
> I'm writing program to count all of repetition in list.
>
> Looks like
> """""""""""""""""""
>
>
> count([],A,0):-!.
>
> count([A|Tail],A,Y) :-
> count(Tail,A,Z),
> Y is 1 + Z.
>
> count([_|Tail],A,Y) :-
> count(Tail,A,Z),
> Y is Z.
>
>
> and I'm asking count([2,2,3,1,3],A,Y).
>
> I get only answer for first repetition
>
> A=2
> Y=2
>
> What Can I do to count all repetition on the list ?
>
> to get result like this ?:
> result for all uniq numbers
>
> A=2
> Y=2
>
> A=3
> Y=2
>
> A=1
> Y=1
>
>
> Greetins for any help :)
>
> D.
| |
| Jan Wielemaker 2007-02-04, 7:04 pm |
| On 2007-02-04, Thorsten Kiefer <toki782@usenet.cnntp.org> wrote:
> Sied@r wrote:
> I didn't try it, but I think you are missing a cut !!
>
> count([],A,0):-!. <<<<<< I guess that cut is unnecessary, but maybe a performance improvement
Yes, its unnecessary, but no, it won't improve performance. Any not
completely toy Prolog system should find that a clause that unifies to
[] cannot unify to [_|_], so clause indexing already found the
deterministic choice.
> count([A|Tail],A,Y) :-
> count(Tail,A,Z),
> Y is 1 + Z,!. <<<<<<<<<<<here
No. Cuts mut be *as early as possible*. You know you are at the
right track after the head unification. Thus:
count([A|Tail],A,Y) :- !, << here
count(Tail,A,Z),
Y is 1 + Z.
Just consider this choicepoint is still around on the recursive call,
so the next recursion will generate another one, etc. Each recursion
level of a counted element will create a choicepoint, all of them to
be destroyed at the very end.
> count([_|Tail],A,Y) :-
> count(Tail,A,Z),
> Y is Z.
Why not
count([_|Tail],A,Z) :-
count(Tail,A,Z).
Shorter, last-call optimization.
Cheers --- Jan
|
|
|
|
|