Question

Prolog questions. Thanks!

c) Write a predicate that removes consecutive duplicate elements from a list. 2- remove_consecutive_duplicates L= [a,b,c,a,e,d,a,c]. (Ia,a,b,c,c,a,a,a,e, d,d,a,cl. L)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is your prolog predicate. Let me know if you have any issue in comments.

% remove_consecutive_duplicates(L1,L2) :- the list L2 is obtained from the list L1 by
% removing repeated occurrences of elements into a single copy
% of the element.
% (list,list) (+,?)

remove_consecutive_duplicates([],[]).
remove_consecutive_duplicates([X],[X]).
remove_consecutive_duplicates([X,X|Xs],Zs) :- remove_consecutive_duplicates([X|Xs],Zs).
remove_consecutive_duplicates([X,Y|Ys],[X|Zs]) :- X \= Y, remove_consecutive_duplicates([Y|Ys],Zs).

Sample Run: -

% remove-consecutive-duplicates(L1,し2) :-the list L2 is obtained from the list L1 removing repeated occurrences of elements i

Add a comment
Know the answer?
Add Answer to:
Prolog questions. Thanks! Write a predicate that removes consecutive duplicate elements from a list. ?- remove_consecutive_duplicates...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT