程序代写 CPS 721 Quiz 4

CPS 721 Quiz 4
2021 Practice Version
(not shared) Switch account
Removing the last element of a list

Copyright By PowCoder代写 加微信 powcoder

The predicate removeLast(List1, List2) succeeds if List2 is equivalent to List1 with the final element removed.
For example, the query ?- removeLast([a,b,c], [a,b]) will succeed, but the query ?- removeLast([a,b,c], [b,a]) will fail.
Note that the query ?- removeLast([ ], X) will fail, since the first list does not contain any elements to remove.
Select the base case and the recursive case from the options below which constitute a correct implementation of the predicate removeLast(List1, List2). (Select exactly 2, the correct answers will be adjacent).
removeLast([ ], [ ]).
removeLast([H|T], Result):- removeLast(T, [H|Result]). removeLast([X], [ ]).
removeLast([H1,H2|T], [H1|Result]):- removeLast([H2|T], Result). removeLast([H|T], [H]).
removeLast([H|T], [H|Result]):- removeLast([H|T], Result).

Removing all occurrences of an element from a list
The predicate removeAll(List, X, Result) succeeds if Result is obtained by removing all occurrences of element X from List.
For example, the query ?- removeAll([a,b,a,c], a, [b,c]) will succeed, but the query ?- removeAll([a,b,a,c], a, [b,a,c]) will fail.
Note that if element X does not occur in List, the query ?- removeAll(List, X, Result) will succeed when Result is equivalent to List.
The base case for this predicate is removeAll([], X, []).
Select the two recursive cases from the options below which constitute a correct implementation of the predicate removeAll(List, X, Result). (Select exactly 2, the correct answers will be adjacent)
removeAll([X|T], X, Result):- removeAll(T, X, Result). removeAll([H|T], X, [H|Result]):- not X = H, removeAll(T, X, Result). removeAll([X1,X2|T], X1, [X2|Result]):- removeAll(T, X1, Result). removeAll([X1,X2|T], X2, [X1|Result]):- removeAll(T, X2, Result). removeAll([X|T], X, T):- removeAll(T, X, T).
removeAll([H|T], X, Result):- removeAll(T, X, [H|Result]).
Removing the rightmost leaf node of a tree
Let Tree1 and Tree2 represent binary tree terms as defined in the lab. Assume that Tree1 is a full binary tree (i.e. all internal nodes have two non-empty children). The predicate removeRightLeaf(Tree1, Tree2) succeeds if Tree2 is equivalent to Tree1 with the rightmost leaf node replaced by the empty tree.
For example, the query ?- removeRightLeaf(tree(a, tree(b, void, void), tree(c, void, void)), tree(a, tree(b, void, void), void)) will succeed, but the query ?- removeRightLeaf(tree(a, tree(b, void, void), tree(c, void, void)), tree(a, void, tree(c, void, void))) will fail.
Note that the query ?- removeRightLeaf(void, X) will fail, since the empty tree ‘void’ does not contain any leaf nodes to remove.

Select the base case and the recursive case from the options below which constitute a correct implementation of the predicate removeRightLeaf(Tree1, Tree2). (Select exactly 2, the correct answers will be adjacent).
removeRightLeaf(tree(X, void, void), void).
removeRightLeaf(tree(X, L, R1), tree(X, L, R2)):- not R1 = void, removeRightLeaf(R1, R2).
removeRightLeaf(void, void).
removeRightLeaf(tree(X, L1, R1), tree(Y, L2, R2)):- not R1 = void, removeRightLeaf(L1, L2), removeRightLeaf(R1, R2).
removeRightLeaf(tree(X, L, R), tree(X, void, void)).
removeRightLeaf(Tree1, Tree2):- not Tree1 = void, removeRightLeaf(tree(X, L, R), Tree2).
Removing all occurrences of an element from a term representation of a list
Let the term ‘next(H, T)’ represent a list where H is the first element and T is itself a list of this format containing all subsequent elements. Let the term ‘nil’ represent the empty list.
The predicate termRemoveAll(TermList, X, Result) operates over term representations of a list as described above. It succeeds if Result is the term list obtained by removing all occurrences of element X from TermList.
For example, the query ?- termRemoveAll(next(a, next(b, next(a, nil))), a, next(b, nil)) will succeed, but the query ?- termRemoveAll(next(a, next(b, next(a, nil))), a, next(b, next(a, nil))) will fail.
Note that if element X does not occur in TermList, the query ?- termRemoveAll(TermList, X, Result) will succeed when Result is equivalent to TermList.
The base case for this predicate is termRemoveAll(nil, X, nil).

Select the two recursive cases from the options below which constitute a correct implementation of the predicate termRemoveAll(TermList, X, Result). (Select exactly 2, the correct answers will be adjacent)
termRemoveAll(next(H, nil), X, nil):- H = X, termRemoveAll(nil, X, nil). termRemoveAll(next(H, T), X, next(T, nil)):- not H = X, termRemoveAll(T, X, nil). termRemoveAll(next(X, T), X, Result):- termRemoveAll(T, X, Result).
termRemoveAll(next(H, T), X, next(H, Result)):- not X = H, termRemoveAll(T, X, Result).
termRemoveAll(next(X, T), X, T):- termRemoveAll(T, X, T).
termRemoveAll(next(H, T), X, Result):- not X = H, termRemoveAll(T, X, next(H, Result)).
Clear form
This form was created inside of . Report Abuse Forms

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com