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: -
Prolog questions. Thanks! Write a predicate that removes consecutive duplicate elements from a list. ?- remove_consecutive_duplicates...
Code in prolog a predicate windows/3 that returns the list of all windows (sublists of consecutive elements) of length 3 of the given list. ?- windows([1,3,a,4,b],Ys). Ys = [[1,3,a],[3,a,4],[a,4,b]] ?- windows([1,3],Ys). Ys = []
Code in Prolog a predicate swapFirstTwo/2 that swaps the rst two elements of the given list. If the list is too short, it must fail. ?- swapFirstTwo([a,b,c,d,e], Ys). Ys = [b,a,c,d,e]. ?- swapFirstTwo([a], Ys). false.
Question 3 (13] (a) Write a procedure filter (L,PredName, L1,L2) that accepts a list L and returns two lists Li and L2, where Li contains all elements in L for which PredName (x) fails, and L2 contains all elements in L for which PredName (x) succeeds. The predicate PredName/1 should be defined when calling the procedure filter. For example: let test be defined as test(N).- N > 0. 7- filter((-6,7,-1,0),test,L1,L2). L1 - (-6.-1) L2 - [7, 0] NB Use the...
PROLOG: Write a prolog predicate that counts occurrences of 'x', 'y', or 'z' in a list. For example, count([x,[y,[a,2],[a,3]],[a,4]], 2) should return true. I only want to count occurrences of x,y,z not a. the base case is that any [a,int] list will have a count of 0. So any sublist with 'a' does not count. another example of what will return true is: count([x,[a,1],[a,5]], 1) The predicate must have the form count(X,Y) where X is the list structure passed and...
In Prolog language, write a recursive predicate to find the last element of a list. You may not use the built-in last predicate in your answer. E.g., ?- lastEle(X,[how,are,you,today]). X=today.
# PrologWrite a predicate that returns the second to last element in a list. ?- second last clement(x, [a.b.c.d,e] x = d?- second last element (X, []) false. ?- second last element (3, [5,4,3,2,1])true.
Write a program in prolog to reverse the elements of a list. Don't call the following library functions that already exist. reverse_list([], []). reverse_list([Head | Tail], Out) :- reverse_list(Tail, TailReversed), append(TailReversed, [Head], Out).
THIS IS A PROLOG QUESTION: Write a Prolog rule to repeat each element of the list TWO times. Hint: You should use recursion. For example, repeat_elements([a,b,c], Result) should return Result as [a,a,b,b,c,c] The result of repeating an empty list [] is an empty list [] so your base case should be the following: repeat_elements([], []). % This is the base case Once you complete your rule by adding recursion rule to the base case, you should test it by a...
1. Write a Prolog program that returns the length of a list of numbers. For example: size([1, 2, 3, 4], len). then return len=4. 2. Write a Prolog program that reverses the given list. For example: reverse([a, b, c, d], X). then return X=[d, c, b, a].
This is in C++. (10 points) Write a de-duplication function that iteratively sanitizes (removes) all consecutive duplicates in a C++ string. Consecutive duplicates are a pair of duplicate English alphabets sitting next to each other in the input string. Example: "AA", "KK", etc., are all consecutive duplicates. This function will internally run as many iterations as needed to remove all consecutive duplicates until there is either no consecutive duplicates left, or the string becomes empty (in which the function returns...