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.
The following is the required Prolog Predicate.
swapFirstTwo([A,B|T],[B,A|T]).
The answer seems really simple as the answer is actually very simple.
As you can see the functor here is swapFirstTwo/2 as instructed in the question, that is the predicate has an arity of 2.
The first argument here is [A,B|T] and the second argument is simply [B,A|T] , which simply means that it is the same list but with the first two elements swapped.
A and B represents the first and the second elements of the list respectively, and T represents the remaining elements of the list.
Also, the predicate will return false if the number of elements in the list is less than 2.
Note:- A, B and T needs to be in capital. This denotes that A,B and T are variables. if these characters are not written in capital the predicate would not work correctly.
Below is a list of some sample outputs:-
?- swapFirstTwo([],Ys).
false.
?- swapFirstTwo([a],Ys).
false.
?- swapFirstTwo([z],Ys).
false.
?- swapFirstTwo([a,b],Ys).
Ys = [b, a].
?- swapFirstTwo([a,1],Ys).
Ys = [1, a].
?- swapFirstTwo([a,1,b],Ys).
Ys = [1, a, b].
?- swapFirstTwo([2,1,b],Ys).
Ys = [1, 2, b].
?- swapFirstTwo([a,b,c,d,e],Ys).
Ys = [b, a, c, d, e].
?- swapFirstTwo([x,y,z,a,b],Ys).
Ys = [y, x, z, a, b].
?- swapFirstTwo([&,1,z,a,b],Ys).
Ys = [1, &, z, a, b].
If you have any query regarding the answer let me know in the comment section, I would love to help.
Code in Prolog a predicate swapFirstTwo/2 that swaps the rst two elements of the given list....
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].
SWI-Prolog a) Write a predicate called lastEle(Atom,List) 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. b) Write a predicate gradeMap(L,R), where L is a list of percentage grade values (integers from 0 to 100), and R is a list of corresponding grade letters (a-f, no +/-'s). You may assume the grade list contains the correct types. E.g., ?-gradeMap([0, 16, 49, 55, 63, 78, 92], R)....
(c++) Write a predicate function that checks whether two vectors have the same elements in the same order. Write a predicate function that checks whether two vectors have the same elements in the same order bool equals(vector<int> a, vector<int> b) provided code : #include <iostream> #include <vector> using namespace std; bool equals(vector<int> a, vector<int> b) { // TODO } int main() { // TODO // Print Expected, "Vectors a and b are " (print "equal." if they are. Otherwise, print...
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].
Python code: 1. This question practices the use of a list method. Assign to the variable grades a list of 10 letter grades from among ‘A’, ‘B’, ‘C’, ‘D’, and ‘F’. For example: grades = [‘A’, ‘F’, ‘C’, ‘F’, ‘A’, ‘B’, ‘A’, ‘C’, ‘A’, ‘B’] Write a Python expression that creates a list named frequency, in which the elements are the number of times each of the letters A, B, C, D and F appear in grades. For example, for...
Solve above using prolog!!
3. 2 marks] Write the rule remdup (List1, List2) that takes as input List1 and produces as output List2. List2 contains all items that appear in List1, except that repeated or duplicate items are not present in List1. For example: ?- remdup([1,2,3] ,x) ?- remdup([3,1,2,31,x) X=[1, 2, 3]. Note that the order of items in List2 is unspecified: for instance, [3,1,2 is an acceptable result for the second example above. 4. 2 marks Consider a course...
C# code Arrays and Linked Lists: Write C++/Java/C#/Python code to declare an array of linked lists of any primitive type you want. (Array of size 2020) (This could be based on MSDN libraries or the lab) – you do not need to instantiate any of the linked lists to contain any actual values. Paste your code for that here (this should only be one line) Based on your code or the lab from 4 or your doubly linked list from...
Please Provide me all Necessary Screenshot with copy able code. Thanks Write a scheme function named up-to-first-number that takes a list as its input and returns a list containing all the elements up to the first numeric element in the input list. You can use the number? predicate function to determine whether an element is a number or not. Sample runs: (up-to-first-number '(a b c d 1 2 3 )) returns (a b c d) (up-to-first-number '(d e f 7))...
1. Two dices are thrown (a) List the elements of the sample space. (b) List the outcomes that define the following events. E: At least one of the dice rolls on 6. he same number . F: Both dice roll on t . G: The sum of the dice is odd H: The number on the first die is larger than the number on the second die. (c) Explain whether the following events are mutually exclusive or not (i) E...