Question

THIS IS A PROLOG QUESTION: Write a Prolog rule to repeat each element of the list...

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 query:

% Test Query:
?- repeat_elements([a,b,c], Result). % Result would be [a, a, b, b, c, c]

Write your complete Prolog rule including both the provided base case and the recursion rule in the following field.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PREDICATES IN PROLOG:

repeat_elements([], []). %base case for empty list
%recursive case for non-empty list
repeat_elements([X|Y1], [X, X|Y2]):- repeat_elements(Y1, Y2).

OUTPUT:

repeat_elements([a,b,c), Result) Result = [a, a, b, b, c, c]

Regards!

Add a comment
Know the answer?
Add Answer to:
THIS IS A PROLOG QUESTION: Write a Prolog rule to repeat each element of the list...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Prolog language, write a recursive predicate to find the last element of a list. You...

    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.

  • SWI-Prolog a) Write a predicate called lastEle(Atom,List) to find the last element of a list. You...

    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)....

  • PROLOG: Write a prolog predicate that counts occurrences of 'x', 'y', or 'z' in a list....

    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...

  • Incorrect Question 17 0/5 pts Complete the second rule of the following PROLOG program for last/2...

    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)

  • Python Write a function index_last(elem, seq) that takes as inputs an element elem and a sequence...

    Python Write a function index_last(elem, seq) that takes as inputs an element elem and a sequence seq, and that uses recursion (i.e., that calls itself recursively) to find and return the index of the last occurrence of elem in seq. The sequence seq can be either a list or a string. If seq is a string, elem will be a single-character string; if seq is a list, elem can be any value. Don’t forget that the index of the first...

  • 4. [201 Use Prolog (e.g., SWI-Prolog) to create a knowledge base for the family tree of...

    4. [201 Use Prolog (e.g., SWI-Prolog) to create a knowledge base for the family tree of Figure 1 and then ask queries about the family tree. Assume the intended interpretation of all predicates of the form p(x.y) is that "x is the p of y" George eMum r-Kydd Elizabeth Philip Margaret Diana-Charles Anne-Mark Andrew Sarah E William Harry Peter Zara Beatrice Eugenie Figure 1. A typical family tree. The symbol-connects spouses and arrows point to children. Enter the information from...

  • ///Program needs to write in prolog/// 6. A binary tree is either empty or it is...

    ///Program needs to write in prolog/// 6. A binary tree is either empty or it is composed of a root element and two successors, which are binary trees themselves. In Prolog we represent the empty tree by the atom 'nil' and the non-empty tree by the term t(X,L,R), where X denotes the root node and L and R denote the left and right subtree, respectively. The following Prolog term represents the given binary tree below. T1 = t(a,t(b,t(d,nil,nil),t(e,nil,nil)),t(c,nil,t(f,t(g,nil, nil),nil))) d...

  • Question 3 (13] (a) Write a procedure filter (L,PredName, L1,L2) that accepts a list L and...

    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...

  • Draw a sketch of a singly linked list containing the following int values: 3, 1, 4,...

    Draw a sketch of a singly linked list containing the following int values: 3, 1, 4, 1, 5. The 3 should be at the front of the list. Remember to sketch the head pointer, each node, and each node's next pointer. 2. Write the code for the following new member function for our SinglyLinkedList class. You should write out the definition of the function, but do not need to write out all of the rest of the class. Your code...

  • Write a Java class myLinkedList to simulate a singly linked list using arrays as the underlying...

    Write a Java class myLinkedList to simulate a singly linked list using arrays as the underlying structure. Include the following methods: 1. insert an element within the linked list.(this should also work for the front and the rear of the list) 2. Remove an element from the linked list 3. Display (print) the elements of the linked list in order. 4. A method to check if the list is "empty". Test your solution using a linked list that initially has...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT