Prolog programming help with 5 and 6 Write a program that re

Prolog programming, help with 5 and 6.

Write a program that removes duplicates from a list. For example: ?- dedup([a, b, c, c, d, e, d, e, f, f, f, f, f], X). X = [a, b, c, d, e, f]. Write a program that counts the number of unique values on a list. For example: ?- countUnique([a, b, c, c, d, e, d, e, f, f, f, f, f], X). X = 6 .

Solution

Answer

5

  % take an empty list with set().  set([], []).    % Place the head in the result,  % and remove all occurrences of the head from the tail,  % make a set (i.e. a list) out of it.  set([h|t], [h|t1]) :-       remv(h, t, t2),      set(t2, t1).    % Remove anything from an the list so that it gives an empty list.  remv(_, [], []).    % suppose the head is an element that we want to remove,  % don\'t keep the head in the list and  % remove the element from the tail to get the new list, this will remove duplicates.  remv(x, [x|t], t1) :- remv(x, t, t1).    % If the head is NOT the element we want to remove,  % keep the head and  % remove the element from the tail to get the new tail.  remv(x, [h|t], [h|t1]) :-      x \\= h,      remv(x, t, t1).  
Prolog programming, help with 5 and 6. Write a program that removes duplicates from a list. For example: ?- dedup([a, b, c, c, d, e, d, e, f, f, f, f, f], X). X

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site