Quantcast
Channel: Prolog return a list which contains only elements which are equal to head of the list - Stack Overflow
Viewing all articles
Browse latest Browse all 3

Prolog return a list which contains only elements which are equal to head of the list

$
0
0

Hello I would like to ask a doubt I have with the following code:

principio([],[]).
principio([H],[H]).
principio([H,_|_],[H]).
principio([H,H|C],P) :-
    principio([H|C],R),P=[H|R].

I would like a way to get from:

?- principio([222,333,101,202,12,222,13,222],X).

X = [222,222,222]

But in this moment I get just the head:

X = [222]

So, to keep it clear I'd like: all successive occurrences of the first element as a list.

My doubt is what does this assignment P=[H|R] why not to put just:

principio([H,H|C],P) :-
    principio([H|C],P)

Also, how would you try to modify this to get the result I asked for? Thank you


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images