Question

In: Computer Science

Please explain this prolog code line by line. union([X|Y],Z,W) :- member(X,Z), union(Y,Z,W). union([X|Y],Z,[X|W]) :- \+ member(X,Z),...

Please explain this prolog code line by line.

union([X|Y],Z,W) :- member(X,Z), union(Y,Z,W).

union([X|Y],Z,[X|W]) :- \+ member(X,Z), union(Y,Z,W).

union([],Z,Z).

Solutions

Expert Solution

Code1 - union([X|Y],Z,W) :- member(X,Z), union(Y,Z,W)

Explainaton - [X|Y] is simply a list with head(first element) is X and tail( all the other elements) is Y.

union(L1,L2,L) denotes the union of L1 and L2 producing the output L. As example union([1,2,3,4],[1,a,b,4],A) will produce the output A = [2,3,1,a,b,4].

Now the code union([X|Y],Z,W) :- member(X,Z), union(Y,Z,W) will perform a union of a list with head X and tail Y with list Z and the output will be W.Now if X is already a member of list Z " member(X,Z) " , then there is no need to include X in the union. Hence, union(Y,Z,W) union of list Y with Z giving the output W.

Code2 - union([X|Y],Z,[X|W]) :- \+ member(X,Z), union(Y,Z,W)

Explainaton - code union([X|Y],Z,[X|W]) :- \+ member(X,Z), union(Y,Z,W) will perform a union of a list with head X and tail Y with list Z and the output will be list W with head X. " \+ " means not , \+ member(X,Z) means X is not the member of Z list , then X will also be included in the union, Thats's why X is head of output list W. Hence, union(Y,Z,W)​​​​​​​, union of Y with Z giving the output W whose head is X.

Code3 - union([],Z,Z)

Explaination - After taking all the elements(Head and tail elements) from [X|Y] it has become void. Hence, union([],Z,Z) union of the empty list with Z will return an output of Z.

Please let me know if you have any other doubts, and if you like the answer a thumbs up would be nice.


Related Solutions

The following logic function is given as a sum of minterms F(W,X,Y,Z) = ∑W,X,Y,Z(7,8,10,11,13) + D(5,...
The following logic function is given as a sum of minterms F(W,X,Y,Z) = ∑W,X,Y,Z(7,8,10,11,13) + D(5, 9, 15). (25 points) a) Draw the K-Map and find the minimal sum-of-products expression for this function. b) Draw the circuit implementing this expression c) Give all input pair or pairs where transition between them would create a timing hazard d) Draw the timing diagram showing the glitch corresponding to the pair or one of the pairs. Assume ALL gate delays are equal e)...
Compute the line integral of the vector field F(x, y, z) = ⟨−y, x, z⟩ along...
Compute the line integral of the vector field F(x, y, z) = ⟨−y, x, z⟩ along the curve which is given by the intersection of the cylinder x 2 + y 2 = 4 and the plane x + y + z = 2 starting from the point (2, 0, 0) and ending at the point (0, 2, 0) with the counterclockwise orientation.
Compute the derivative of the given vector field F. Evaluate the line integral of F(x,y,z) = (y+z+yz , x+z+xz , x+y+xy )
Compute the derivative of the given vector field F. Evaluate the line integral of F(x,y,z) = (y+z+yz , x+z+xz , x+y+xy )over the path C consisting of line segments joining (1,1,1) to (1,1,2), (1, 1, 2) to (1, 3, 2), and (1, 3, 2) to (4, 3, 2) in 3 different ways, along the given path, along the line from (1,1,1) to (4,3,2), and finally by finding an anti-derivative, f, for F.
If x = 5, y = 6, z = 4, and w = 3.5, evaluate each...
If x = 5, y = 6, z = 4, and w = 3.5, evaluate each of the following statements, if possible. If it is not possible, state the reason. ((++y) * z) % x (x % y) % (--z) a)     Static_cast<int> ((--y) + x / z) Static_cast<int> (w + Static_cast<double> (x / z + w))
Proposition 8.59. Suppose that X, Y, W, Z, A, B are sets. Let f : X...
Proposition 8.59. Suppose that X, Y, W, Z, A, B are sets. Let f : X → Y , W ⊆ X, Z ⊆ X, A ⊆ Y , and B ⊆ Y . Then the following are true: prove the following ? (1) f(W ∩ Z) ⊆ f(W) ∩ f(Z). (2) f(W ∪ Z) = f(W) ∪ f(Z). (3) f−1(A ∩ B) ⊆ f−1(A) ∪ f−1(B) 4) f−1(A ∪ B) = f−1(A) ∪ f−1(B). (5) X−f−1(A)⊆f−1(Y −A). (6) W...
please let me know reference of this MATLAB code. please explain this code line by line....
please let me know reference of this MATLAB code. please explain this code line by line. . . N=256; %% sampling number n=linspace(0,1,N); fs=1/(n(2)-n(1)); x=5*(sin((2*pi*10*n))); %% create signal N=length(x); f=[-fs/2:fs/N:fs/2-fs/N]; subplot(211) plot(f,fftshift(abs(fft(x)))); title('|G(f)|');grid; xlabel('frequency'); ylabel('|G(f)|'); %Zero padding xx=[zeros(1,128) x zeros(1,128)]; N=length(xx); f=[-fs/2:fs/N:fs/2-fs/N]; subplot(212) plot(f,fftshift(abs(fft(xx)))); title('|Gz(f)|');grid; xlabel('frequency'); ylabel('|Gz(f)|');
The curried version of let f (x,y,z) = (x,(y,z)) is let f (x,(y,z)) = (x,(y,z)) Just...
The curried version of let f (x,y,z) = (x,(y,z)) is let f (x,(y,z)) = (x,(y,z)) Just f (because f is already curried) let f x y z = (x,(y,z)) let f x y z = x (y z)
Let X, Y ⊂ Z and x, y ∈ Z Let A = (X\{x}) ∪ {x}....
Let X, Y ⊂ Z and x, y ∈ Z Let A = (X\{x}) ∪ {x}. a) Prove or disprove: A ⊆ X b) Prove or disprove: X ⊆ A c) Prove or disprove: P(X ∪ Y ) ⊆ P(X) ∪ P(Y ) ∪ P(X ∩ Y ) d) Prove or disprove: P(X) ∪ P(Y ) ∪ P(X ∩ Y ) ⊆ P(X ∪ Y )
If X, Y and Z are three arbitrary vectors, prove these identities: a. (X×Y).Z = X.(Y×Z)...
If X, Y and Z are three arbitrary vectors, prove these identities: a. (X×Y).Z = X.(Y×Z) b. X×(Y×Z) = (X.Z)Y – (X.Y)Z c. X.(Y×Z) = -Y.(X×Z)
Assume the following preference order for four goods, w, x, y, and z, of a total...
Assume the following preference order for four goods, w, x, y, and z, of a total of 26 people: # of people 9 6 2 4 5 W X Y Y Z Z Y X Z X X Z Z X Y Y W W W W You want to extract the will of the group and employ different rules. What is the group's preference order when you employ: A) Plurality Vote B) Condorcet Vote C) Borda Count D) "Vote...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT