I am in the process of learning Prolog and I'm have trouble approaching the language coming from the object-oriented background.
I am attempted to complete the following instructions:
Implement a phased queue. This is a structure consisting of two lists: the front end and the back end, represented as
queue(Front, Back). The queue is empty iff both lists are empty. If elements are added to the queue they are added to the back end; if they are removed they are removed from the front end; if the front end becomes empty (and the back end is not empty) then the back end becomes the new front end with[]as the new back end. For example, we could start with a queue likequeue([2,5,7], []), adding 8 gives usqueue([2,5,7], [8]), removing two elements givesqueue([7], [8]), adding a 9 givesqueue([7], [9,8]), and removing an element givesqueue([9,8], [])
I don't understand how I create and then refer to a queue structure in the .pl file, in a way that other predicates can then manipulate and transform
I have sketched out roughly what I believe I should be doing, both defined queue structure and as just a list of lists.
add_to_q(X, [[H|T]|[H2|T2]], [[H|T]|[X|[H2|T2]]).
queue(X, Y)
add_to_q(A, queue(X,Y), queue(X, [A|Y]). % gives Syntax error: Operator expected
------------------
remove_from_q( [[H | [T|T3]] | [H2|T2]], [[T|T3]] | [H2|T2]]).
queue(X, Y)
remove_from_q( queue(X,[H|T]), queue(H,T).
How do I define and work with a structure in Prolog, how would I add what would be in a OO language methods such as getHead or getTail, I have seen examples of how you would do this with just lists but I am not working with a list of lists rather a "queue" of two separate lists?
Feeling lost!