# Prolog
Write 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.
There are many ways to return a second to last item in a
list.
1.
secondLast(L, X) :-
append(_, [X, _], L).
This predicate can get you the second last item in a list.
2.
secondLast([X,_], X).
secondLast([_|T], X) :- secondLast(T, X).
This predicate also gets you the second last item in a list.
Here the first statement (secondLast([X,_], X).) says x is an
element which is in a position of second to last in a list [X,_],
which is true. _ means the last item which we dont need.
The second statement (secondLast([_|T], X) :- secondLast(T, X).)
states that X is the second to last element of the list, T. This is
also true of any list that is 3 or more elements.
Write a predicate that returns the second to last element in a list.
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.
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...
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 = []
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].
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)
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...
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].
Write a function which takes a list and returns frequencies of each element in the list? For example: L=[‘a’,’b’,’a’,’c’,’d’,’b’] Returns ‘a’: 2, ‘b’: 2, ‘c’: 1, ‘d’: 1
Incorrect Question 17 0/5 pts Complete the second rule of the following PROLOG program for last/2 where last(X,L) checks whether X is the last element of the list L. last(X,[X]). last(X,[LIT]):- (A) last([). (B) last(X,_). (C) last(X,T). (D) None of these (A) (B) (C) (D)