Re: Help with lists.
- From: student <not@xxxxxxxxxxxxxxxx>
- Date: Fri, 19 Aug 2005 21:07:47 GMT
joonix wrote:
[snip]Greetings,
I have just started learning prolog. I come from a C/C++/Java background and I am having a very hard time getting my head around this style of programming.
Can some one please offer me some assistance in creating the following
2) A predicate no-triplicates(List1, List2) that replaces all occurrences of any element that appears three or more times in List1 (i.e., triplicate members) with a single occurrence of that element to generate List2. This single occurrence must occupy the position in the list held by the first occurrence of the triplicate member in List1. Duplicate occurrences are left unchanged. For instance, if the input list List1 is [1, 2, 3, 2, 3, 3, 4], then the output list List2 must be [1, 2, 3, 2, 4].
Thanks in advance,
joo.
Another possibility:
reduce_triples([],[]).
reduce_triples([X|L1],[X|L2]):-
( remove(X,L1,LX), remove(X,LX,LXX) ->
removeall(X,LXX,L0), reduce_triples(L0,L2) ;
reduce_triples(L1,L2) ).remove(X,[Y|L1],L2) :-
( X==Y -> L2=L1 ;
remove(X,L1,L0), L2=[Y|L0] ).removeall(_,[],[]).
removeall(X,[Y|L1],L2) :-
( X==Y -> removeall(X,L1,L0), L2=L0 ;
removeall(X,L1,L0), L2=[Y|L0] ).test :- data(List1), reduce_triples(List1,List2), write(List2),nl, fail. test.
data([1, 2, 3, 2, 3, 3, 4]). data([4,3,2,1,4,3,2,4,3,4]). data([4,5,5,3,2,1,5,3,4,2,4,5,3,5,4]).
?- test. [1, 2, 3, 2, 4] [4, 3, 2, 1, 2] [4, 5, 3, 2, 1, 2]
-- billh .
- Follow-Ups:
- Re: Help with lists.
- From: Bart Demoen
- Re: Help with lists.
- References:
- Help with lists.
- From: joonix
- Help with lists.
- Prev by Date: Re: File names and consult.
- Next by Date: Re: Help with lists.
- Previous by thread: Re: Help with lists.
- Next by thread: Re: Help with lists.
- Index(es):
Relevant Pages
|
|