Write a program in prolog to reverse the elements of a list. Don't call the following library functions that already exist.
reverse_list([], []). reverse_list([Head | Tail], Out) :- reverse_list(Tail, TailReversed), append(TailReversed, [Head], Out).
Program
'reverse/2' .
/* The following is an implementation of reverse/2 that I just invented that does not suffer from a non-termination problem for reverse(P,[]) . */
reverse(_source_,_target_) :-
reverse(_source_,_target_,_source_,_target_,[],[]) .
reverse(_source_,_target_,[],[],_target_,_source_) .
reverse(_source_,_target_,[_source_car_|_source_cdr_],[_target_car_|_target_cdr_],_source_collect_,_target_collect_)
:-
reverse(_source_,_target_,_source_cdr_,_target_cdr_,[_source_car_|_source_collect_],[_target_car_|_target_collect_])
.
Testing:
/* ?- reverse([],Q) . Q = [] ?- reverse([a],Q) . Q = [a] ?- reverse([a,b],Q) . Q = [b,a] ?- reverse([a,b,c],Q) . Q = [c,b,a]
*/
Hope this helps!
Write a program in prolog to reverse the elements of a list. Don't call the following...
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 Prolog program to find the maximum, minimum, and range of values in a list of numbers. Please also provide the program with output.
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].
Write two clauses in PROLOG that determines if there are three values in a list that sum up to a value of N. The output should be a single true if there exist three values whose sum is N or a single false if there are not. If there are multiple values whose sum is N then the program should only print true once.
Write an algorithm and a C++ program that reverse the order of the elements of an array that is assumed to have been previously entered ("the first will be the last ..."). The reversion must be performed without the usage of any intermediary array.
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...
1. Explain the function/purpose of the following sequence of program statements by expressing the postcondition and then prove that the program is correct using the axiomatic verification method. Precondition: {x = A and y = B} t=x x=y y=t Postcondition:________________ 2. Prove that the following grammar is ambiguous. <stmt> -> <assign> | <if-stmt> <assign> -> <id> := <expr> <if-stmt> -> if <bool> then <stmt> | if <bool> then <stmt> else <stmt> Modify the grammar above to make it unambiguous: 3....
Python Question? Write a function called reverse(user_list, num = -1) that takes a list and a number as parameters. The function returns a copy of the list with the first number of items reversed. Conditions: The number parameter must be a default argument. - If the default argument for number is given in the function call, only the first number of items are reversed. - If the default argument for number is not provided in the function call, then the...
In java Write a method public void printReverse() that prints the elements of a doubly linked list in reverse. Write a method public void delete5FromTheEnd() which deletes the 5th element from end of the list. Note that if you reach the end then you have to reverse the direction of counting. In the main() method of the test class, create a randomly generated Doubly-Linked list of 10 Integers. Next, call the delete5FromTheEnd() method and print the lists iteratively until the...
In java Write a method public void printReverse() that prints the elements of a doubly linked list in reverse. Write a method public void delete5FromTheEnd() which deletes the 5th element from end of the list. Note that if you reach the end then you have to reverse the direction of counting. In the main() method of the test class, create a randomly generated Doubly-Linked list of 10 Integers. Next, call the delete5FromTheEnd() method and print the lists iteratively until the...