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.
%The basic idea is when there is only one element in a list that is our requirement and hence the base case.
%we recursively call on the remaining part of the list until this base case is reached.
%The following is the prolog code for the same.
lastEle(Y,[Y]).
lastEle(X,[Head|R]):-lastEle(X,R).
%Please refer to the above image for execution on sample test cases.
%Please rate the solution. Also, don't hesitate to ask any doubts in the comments section below.
In Prolog language, write a recursive predicate to find the last element of a list. You...
# 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.
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...
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...
In Prolog, define the isUnion predicate so that isUnion(X,Y,Z) says that the union of X and Y is Z. Do not use the predefined list predicates. Your predicate may choose a fixed order for Z. If you query isUnion([1,2],[3],Z) it should find a binding for Z, but it need not succeed on both isUnion([1],[2],[1,2]) and isUnion([1],[2],[2,1]). Your predicate need not work well when X or Y are unbound variables.
The predicate minim(IntList,Min) is true if Min is the minimum of the integers in a given non-empty list IntList. For example, a query ?- minim([29,1,8,167], X). Returns the answer X = 1, a query ?- minim([12,123,456,12,78,999,123,12],X), returns the answer X = 12, but a query ?- minim([99,2,17,155],17), must return the answer no. Write a recursive program that implements this predicate using any arithmetical operators, but you cannot use any auxiliary predicates. (Must use PROLOG)
Prolog questions. Thanks! Write a predicate that removes consecutive duplicate elements from a list. ?- remove_consecutive_duplicates ([a, a, b, c, c, a, a, a, e, d, d, a, c], L). L = [a, b, c,,a, e, d, a, c].
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...
Define a predicate function mymember in Scheme language (DrRacket) that takes an atom and a simple list; returns #t if the atom is in the list; #f otherwise. You are not allowed to use the built-in member function. Provide two test cases for this function.
Scheme number computer a. Write a recursive procedure (digits n) that computes the number of digits in the integer n using a recursive process. For example, (digits 42) should return 2 and (digits 13579) should return 5. You may make use of the built in floor predicate for truncating decimals to whole numbers. b. Rewrite your procedure from part (a) using an iterative process. Call the function (digits-iter n).
Write a static recursive method that removes all occurrences of the element from an array list. public static void removeAllOccurrences(ArrayList<Integer> list, Integer element)