A queue is a first-in, first-out store of information. This implementation of queues uses difference-lists, the head of the difference-list represents the beginning of the queue and the tail represents the end of the queue. The members of the difference-list are the elements in the queue. The first argument in the queue-representation is the number of elements in the queue in unary representation.
Thus, a queue with n elements is represented as follows:
q(s(...s(0)...), [X1,...,Xn,Y1,...,Ym], [Y1,...,Ym])
where n is the length of the queue and X1...Xn are the elements of the queue.
To load the package, enter the query
| ?- use_module(library(queues)).
empty_queue(
?Queue)
is_queue(
+Queue)
queue(
?X,
?Queue)
queue_head(
?Head,
?Queue1,
?Queue2)
| ?- queue_head(Head, Nq, q(s(s(s(s(0)))),[1,2,3,4|R],R)). Head = 1, Nq = q(s(s(s(0))),[2,3,4|_193],_193), R = _193
queue_head_list(
+HeadList,
?Queue1,
?Queue2)
queue_last(
?Last,
?Queue1,
?Queue2)
queue_last_list(
+LastList,
?Queue1,
?Queue2)
| ?- queue_last_list([5,6], q(s(s(0)))), [1,2|R], R), NQ). NQ = q(s(s(s(s(0)))))),[1,2,5,6|_360],_360), R = [5,6|_360]
list_queue(
+List,
?Queue)
| ?- list_queue([1,2,3,4], Q). Q = q(s(s(s(s(0)))),[1,2,3,4|_138],_138)
queue_length(
+Queue,
?Length)
| ?- queue_length(q(s(s(s(s(s(0))))),[a,b,c,d,e|R],R), L). L = 5, R = _155