1 Queues
Following queue structures implement and provide the functions empty?, enqueue, head, tail, queue and queue->list. All the queue structures are polymorphic.
1.1 Banker’s Queue
(require pfds/queue/bankers) | package: pfds |
A Queue is nothing but a FIFO data structure. A Banker’s Queue is a amortized queue obtained using Bankers method. It provides a amortized running time of O(1) for head, tail and enqueue operations. To obtain this amortized running time, the data structure uses the techniques, lazy evaluation and memoization. Banker’s Queue internally uses Streams for lazy evaluation. For Streams, see Streams
syntax
(Queue A)
> (queue 1 2 3 4 5 6)
- : #(struct:Queue
((Rec
g285461
(U (Pairof Positive-Byte g285461) (Promiseof g285461) Null))
Integer
(Listof Positive-Byte)
Integer))
#<Queue>
In the above example, the queue obtained will have 1 as its head element.
In the above example, (enqueue 10 (queue 4 5 6)) enqueues 10 to the end of the queue and returns (queue 4 5 6 10).
> (tail (queue 4 5 6))
- : #(struct:Queue
((Rec
g285599
(U (Pairof Positive-Byte g285599) (Promiseof g285599) Null))
Integer
(Listof Positive-Byte)
Integer))
#<Queue>
> (tail (empty Integer)) tail: given queue is empty
In the above example, (tail (queue 4 5 6)), returns (queue 5 6).
procedure
(queue->list que) → (Queue A)
que : (Queue A)
> (queue->list (queue 10 2 34 4 15 6)) - : (Listof Positive-Byte)
'(10 2 34 4 15 6)
> (queue->list (empty Integer)) - : (Listof Integer)
'()
> (queue->list (map add1 (queue 1 2 3 4 5 6))) - : (Listof Positive-Index)
'(2 3 4 5 6 7)
> (queue->list (map * (queue 1 2 3 4 5 6) (queue 1 2 3 4 5 6))) - : (Listof Positive-Index)
'(1 4 9 16 25 36)
procedure
(fold func init que1 que2 ...) → C
func : (C A B ... B -> C) init : C que1 : (Queue A) que2 : (Queue B)
fold currently does not produce correct results when the given function is non-commutative.
> (fold + 0 (queue 1 2 3 4 5 6)) - : Integer [more precisely: Nonnegative-Integer]
21
> (fold * 1 (queue 1 2 3 4 5 6) (queue 1 2 3 4 5 6)) - : Integer [more precisely: Positive-Integer]
518400
> (define que (queue 1 2 3 4 5 6)) > (queue->list (filter (λ: ([x : Integer]) (> x 5)) que)) - : (Listof Positive-Byte)
'(6)
> (queue->list (filter (λ: ([x : Integer]) (< x 5)) que)) - : (Listof Positive-Byte)
'(1 2 3 4)
> (queue->list (filter (λ: ([x : Integer]) (<= x 5)) que)) - : (Listof Positive-Byte)
'(1 2 3 4 5)
> (queue->list (remove (λ: ([x : Integer]) (> x 5)) (queue 1 2 3 4 5 6))) - : (Listof Positive-Byte)
'(1 2 3 4 5)
> (queue->list (remove (λ: ([x : Integer]) (< x 5)) (queue 1 2 3 4 5 6))) - : (Listof Positive-Byte)
'(5 6)
> (queue->list (remove (λ: ([x : Integer]) (<= x 5)) (queue 1 2 3 4 5 6))) - : (Listof Positive-Byte)
'(6)
procedure
(andmap func que1 que2 ...) → Boolean
func : (A B ... B -> Boolean) que1 : (Queue A) que2 : (Queue B)
> (andmap even? (queue 1 2 3 4 5 6)) - : Boolean
#f
> (andmap odd? (queue 1 2 3 4 5 6)) - : Boolean
#f
> (andmap positive? (queue 1 2 3 4 5 6)) - : Boolean
#t
> (andmap negative? (queue -1 -2)) - : Boolean
#t
procedure
(ormap func que1 que2 ...) → Boolean
func : (A B ... B -> Boolean) que1 : (Queue A) que2 : (Queue B)
> (ormap even? (queue 1 2 3 4 5 6)) - : Boolean
#t
> (ormap odd? (queue 1 2 3 4 5 6)) - : Boolean
#t
> (ormap positive? (queue -1 -2 3 4 -5 6)) - : Boolean
#t
> (ormap negative? (queue 1 -2)) - : Boolean
#t
procedure
(build-queue size func) → (Queue A)
size : Natural func : (Natural -> A)
> (queue->list (build-queue 5 (λ:([x : Integer]) (add1 x)))) - : (Listof Integer)
'(1 2 3 4 5)
> (queue->list (build-queue 5 (λ:([x : Integer]) (* x x)))) - : (Listof Integer)
'(0 1 4 9 16)
> (head+tail (queue 1 2 3 4 5))
- : (Pairof
Positive-Byte
#(struct:Queue
((Rec
g286262
(U (Pairof Positive-Byte g286262) (Promiseof g286262) Null))
Integer
(Listof Positive-Byte)
Integer)))
'(1 . #<Queue>)
> (head+tail (build-queue 5 (λ:([x : Integer]) (* x x))))
- : (Pairof
Integer
#(struct:Queue
((Rec g286285 (U (Pairof Integer g286285) (Promiseof g286285) Null))
Integer
(Listof Integer)
Integer)))
'(0 . #<Queue>)
> (head+tail (empty Integer)) head+tail: given queue is empty
1.2 Physicist’s Queue
(require pfds/queue/physicists) | package: pfds |
A Queue is nothing but a FIFO data structure. A Physicist’s queue ia a Amortized queues obtained by Physicist’s method. Provides a amortized running time of O(1) for head, tail and enqueue operations. Physicists’s Queue uses lazy evaluation and memoization to get this amortized running time.
syntax
(Queue A)
> (queue 1 2 3 4 5 6)
- : #(struct:Queue
((Listof Integer)
(Boxof (U (-> (Listof Integer)) (Listof Integer)))
Integer
(Listof Integer)
Integer))
#<Queue>
In the above example, the queue obtained will have 1 as its head element
In the above example, enqueue adds the element 10 to (queue 1 2 3 4 5 6) and returns (queue 1 2 3 4 5 6 10).
In the above example, (tail (queue 1 2 3 4 5 6)), returns (queue 2 3 4 5 6).
procedure
(queue->list que) → (Queue A)
que : (Queue A)
> (queue->list (queue 10 2 34 4 15 6)) - : (Listof Integer)
'(10 2 34 4 15 6)
> (queue->list (empty Integer)) - : (Listof Integer)
'()
> (queue->list (map add1 (queue 1 2 3 4 5 6))) - : (Listof Integer)
'(2 3 4 5 6 7)
> (queue->list (map * (queue 1 2 3 4 5 6) (queue 1 2 3 4 5 6))) - : (Listof Integer)
'(1 4 9 16 25 36)
procedure
(fold func init que1 que2 ...) → C
func : (C A B ... B -> C) init : C que1 : (Queue A) que2 : (Queue B)
fold currently does not produce correct results when the given function is non-commutative.
> (fold + 0 (queue 1 2 3 4 5 6)) - : Integer
21
> (fold * 1 (queue 1 2 3 4 5 6) (queue 1 2 3 4 5 6)) - : Integer
518400
> (define que (queue 1 2 3 4 5 6)) > (queue->list (filter (λ: ([x : Integer]) (> x 5)) que)) - : (Listof Integer)
'(6)
> (queue->list (filter (λ: ([x : Integer]) (< x 5)) que)) - : (Listof Integer)
'(1 2 3 4)
> (queue->list (filter (λ: ([x : Integer]) (<= x 5)) que)) - : (Listof Integer)
'(1 2 3 4 5)
> (queue->list (remove (λ: ([x : Integer]) (> x 5)) (queue 1 2 3 4 5 6))) - : (Listof Integer)
'(1 2 3 4 5)
> (queue->list (remove (λ: ([x : Integer]) (< x 5)) (queue 1 2 3 4 5 6))) - : (Listof Integer)
'(5 6)
> (queue->list (remove (λ: ([x : Integer]) (<= x 5)) (queue 1 2 3 4 5 6))) - : (Listof Integer)
'(6)
procedure
(andmap func que1 que2 ...) → Boolean
func : (A B ... B -> Boolean) que1 : (Queue A) que2 : (Queue B)
> (andmap even? (queue 1 2 3 4 5 6)) - : Boolean
#f
> (andmap odd? (queue 1 2 3 4 5 6)) - : Boolean
#f
> (andmap positive? (queue 1 2 3 4 5 6)) - : Boolean
#t
> (andmap negative? (queue -1 -2)) - : Boolean
#t
procedure
(ormap func que1 que2 ...) → Boolean
func : (A B ... B -> Boolean) que1 : (Queue A) que2 : (Queue B)
> (ormap even? (queue 1 2 3 4 5 6)) - : Boolean
#t
> (ormap odd? (queue 1 2 3 4 5 6)) - : Boolean
#t
> (ormap positive? (queue -1 -2 3 4 -5 6)) - : Boolean
#t
> (ormap negative? (queue 1 -2)) - : Boolean
#t
procedure
(build-queue size func) → (Queue A)
size : Natural func : (Natural -> A)
> (queue->list (build-queue 5 (λ:([x : Integer]) (add1 x)))) - : (Listof Integer)
'(1 2 3 4 5)
> (queue->list (build-queue 5 (λ:([x : Integer]) (* x x)))) - : (Listof Integer)
'(0 1 4 9 16)
> (head+tail (queue 1 2 3 4 5))
- : (Pairof
Integer
#(struct:Queue
((Listof Integer)
(Boxof (U (-> (Listof Integer)) (Listof Integer)))
Integer
(Listof Integer)
Integer)))
'(1 . #<Queue>)
> (head+tail (build-queue 5 (λ:([x : Integer]) (* x x))))
- : (Pairof
Integer
#(struct:Queue
((Listof Integer)
(Boxof (U (-> (Listof Integer)) (Listof Integer)))
Integer
(Listof Integer)
Integer)))
'(0 . #<Queue>)
> (head+tail (empty Integer)) head+tail: given queue is empty
1.3 Implicit Queue
(require pfds/queue/implicit) | package: pfds |
Queues obtained by applying the technique called Implicit Recursive Slowdown. Provides a amortized running time of O(1) for the operations head, tail and enqueue. Implicit Recursive Slowdown combines laziness and technique called Recursive Slow-Down developed by Kaplan and Tarjan in their paper Persistant Lists with Catenation via Recursive Slow-Down.
syntax
(Queue A)
> (queue 1 2 3 4 5 6) - : (U (Deep Positive-Byte) (Shallow Positive-Byte))
#<Deep>
In the above example, the queue obtained will have 1 as its head element.
In the above example, enqueue adds the element 10 to of (queue 1 2 3 4 5 6) and returns (queue 1 2 3 4 5 6 10).
procedure
(queue->list que) → (Queue A)
que : (Queue A)
> (queue->list (queue 10 2 34 4 15 6)) - : (Listof Positive-Byte)
'(10 2 34 4 15 6)
> (queue->list empty) - : (Listof Nothing)
'()
> (queue->list (map add1 (queue 1 2 3 4 5 6))) - : (Listof Positive-Index)
'(2 3 4 5 6 7)
> (queue->list (map * (queue 1 2 3 4 5 6) (queue 1 2 3 4 5 6))) - : (Listof Positive-Index)
'(1 4 9 16 25 36)
procedure
(fold func init que1 que2 ...) → C
func : (C A B ... B -> C) init : C que1 : (Queue A) que2 : (Queue B)
fold currently does not produce correct results when the given function is non-commutative.
> (fold + 0 (queue 1 2 3 4 5 6)) - : Integer [more precisely: Nonnegative-Integer]
21
> (fold * 1 (queue 1 2 3 4 5 6) (queue 1 2 3 4 5 6)) - : Integer [more precisely: Positive-Integer]
518400
> (define que (queue 1 2 3 4 5 6)) > (queue->list (filter (λ: ([x : Integer]) (> x 5)) que)) - : (Listof Positive-Byte)
'(6)
> (queue->list (filter (λ: ([x : Integer]) (< x 5)) que)) - : (Listof Positive-Byte)
'(1 2 3 4)
> (queue->list (filter (λ: ([x : Integer]) (<= x 5)) que)) - : (Listof Positive-Byte)
'(1 2 3 4 5)
> (queue->list (remove (λ: ([x : Integer]) (> x 5)) (queue 1 2 3 4 5 6))) - : (Listof Positive-Byte)
'(1 2 3 4 5)
> (queue->list (remove (λ: ([x : Integer]) (< x 5)) (queue 1 2 3 4 5 6))) - : (Listof Positive-Byte)
'(5 6)
> (queue->list (remove (λ: ([x : Integer]) (<= x 5)) (queue 1 2 3 4 5 6))) - : (Listof Positive-Byte)
'(6)
procedure
(andmap func que1 que2 ...) → Boolean
func : (A B ... B -> Boolean) que1 : (Queue A) que2 : (Queue B)
> (andmap even? (queue 1 2 3 4 5 6)) - : Boolean
#f
> (andmap odd? (queue 1 2 3 4 5 6)) - : Boolean
#f
> (andmap positive? (queue 1 2 3 4 5 6)) - : Boolean
#t
> (andmap negative? (queue -1 -2)) - : Boolean
#t
procedure
(ormap func que1 que2 ...) → Boolean
func : (A B ... B -> Boolean) que1 : (Queue A) que2 : (Queue B)
> (ormap even? (queue 1 2 3 4 5 6)) - : Boolean
#t
> (ormap odd? (queue 1 2 3 4 5 6)) - : Boolean
#t
> (ormap positive? (queue -1 -2 3 4 -5 6)) - : Boolean
#t
> (ormap negative? (queue 1 -2)) - : Boolean
#t
procedure
(build-queue size func) → (Queue A)
size : Natural func : (Natural -> A)
> (queue->list (build-queue 5 (λ:([x : Integer]) (add1 x)))) - : (Listof Integer)
'(1 2 3 4 5)
> (queue->list (build-queue 5 (λ:([x : Integer]) (* x x)))) - : (Listof Integer)
'(0 1 4 9 16)
1.4 Bootstraped Queue
(require pfds/queue/bootstrapped) | package: pfds |
Bootstrapped Queue use a structural bootstrapping technique called Structural Decomposition. The data structure gives a worst case running time of O(1) for the operation head and O(log*(n)) for tail and enqueue. Internally uses Physicist’s Queue.
syntax
(Queue A)
> (queue 1 2 3 4 5 6) - : (U (IntQue Integer) Null)
#<IntQue>
In the above example, the queue obtained will have 1 as its first element.
In the above example, (enqueue 10 (queue 1 2 3 4 5 6)) adds the 10 to the queue (queue 1 2 3 4 5 6). 10 as its last element.
In the above example, (tail (queue 1 2 3 4 5 6)), removes the head of the given queue returns (queue 2 3 4 5 6).
procedure
(queue->list que) → (Queue A)
que : (Queue A)
> (queue->list (queue 10 2 34 4 15 6)) - : (Listof Integer)
'(10 2 34 4 15 6)
> (queue->list empty) - : (Listof Nothing)
'()
> (queue->list (map add1 (queue 1 2 3 4 5 6))) - : (Listof Integer)
'(2 3 4 5 6 7)
> (queue->list (map * (queue 1 2 3 4 5 6) (queue 1 2 3 4 5 6))) - : (Listof Integer)
'(1 4 9 16 25 36)
procedure
(fold func init que1 que2 ...) → C
func : (C A B ... B -> C) init : C que1 : (Queue A) que2 : (Queue B)
fold currently does not produce correct results when the given function is non-commutative.
> (fold + 0 (queue 1 2 3 4 5 6)) - : Integer
21
> (fold * 1 (queue 1 2 3 4 5 6) (queue 1 2 3 4 5 6)) - : Integer
518400
> (define que (queue 1 2 3 4 5 6)) > (queue->list (filter (λ: ([x : Integer]) (> x 5)) que)) - : (Listof Integer)
'(6)
> (queue->list (filter (λ: ([x : Integer]) (< x 5)) que)) - : (Listof Integer)
'(1 2 3 4)
> (queue->list (filter (λ: ([x : Integer]) (<= x 5)) que)) - : (Listof Integer)
'(1 2 3 4 5)
> (queue->list (remove (λ: ([x : Integer]) (> x 5)) (queue 1 2 3 4 5 6))) - : (Listof Integer)
'(1 2 3 4 5)
> (queue->list (remove (λ: ([x : Integer]) (< x 5)) (queue 1 2 3 4 5 6))) - : (Listof Integer)
'(5 6)
> (queue->list (remove (λ: ([x : Integer]) (<= x 5)) (queue 1 2 3 4 5 6))) - : (Listof Integer)
'(6)
procedure
(andmap func que1 que2 ...) → Boolean
func : (A B ... B -> Boolean) que1 : (Queue A) que2 : (Queue B)
> (andmap even? (queue 1 2 3 4 5 6)) - : Boolean
#f
> (andmap odd? (queue 1 2 3 4 5 6)) - : Boolean
#f
> (andmap positive? (queue 1 2 3 4 5 6)) - : Boolean
#t
> (andmap negative? (queue -1 -2)) - : Boolean
#t
procedure
(ormap func que1 que2 ...) → Boolean
func : (A B ... B -> Boolean) que1 : (Queue A) que2 : (Queue B)
> (ormap even? (queue 1 2 3 4 5 6)) - : Boolean
#t
> (ormap odd? (queue 1 2 3 4 5 6)) - : Boolean
#t
> (ormap positive? (queue -1 -2 3 4 -5 6)) - : Boolean
#t
> (ormap negative? (queue 1 -2)) - : Boolean
#t
procedure
(build-queue size func) → (Queue A)
size : Natural func : (Natural -> A)
> (queue->list (build-queue 5 (λ:([x : Integer]) (add1 x)))) - : (Listof Integer)
'(1 2 3 4 5)
> (queue->list (build-queue 5 (λ:([x : Integer]) (* x x)))) - : (Listof Integer)
'(0 1 4 9 16)
1.5 Real-Time Queue
(require pfds/queue/real-time) | package: pfds |
Real-Time Queues eliminate the amortization by employing laziness and a technique called Scheduling. The data structure gives a worst case running time of O(1) for the operations head, tail and enqueue.
syntax
(Queue A)
> (queue 1 2 3 4 5 6)
- : #(struct:Queue
((Rec
g294125
(U (Boxof (U (-> (Pairof Integer g294125)) (Pairof Integer g294125)))
Null))
(Listof Integer)
(Rec
g294128
(U (Boxof (U (-> (Pairof Integer g294128)) (Pairof Integer g294128)))
Null))))
#<Queue>
In the above example, the queue obtained will have 1 as its first element.
In the above example, (enqueue 10 que) adds 10 to the end of (queue 1 2 3 4 5 6) and returns (queue 1 2 3 4 5 6 10).
> (tail (queue 1 2 3 4 5 6))
- : #(struct:Queue
((Rec
g294221
(U (Boxof (U (-> (Pairof Integer g294221)) (Pairof Integer g294221)))
Null))
(Listof Integer)
(Rec
g294224
(U (Boxof (U (-> (Pairof Integer g294224)) (Pairof Integer g294224)))
Null))))
#<Queue>
> (tail (empty Integer)) tail: given queue is empty
In the above example, (tail (queue 1 2 3 4 5 6)), returns (queue 2 3 4 5 6).
procedure
(queue->list que) → (Queue A)
que : (Queue A)
> (queue->list (queue 10 2 34 4 15 6)) - : (Listof Integer)
'(10 2 34 4 15 6)
> (queue->list (map add1 (queue 1 2 3 4 5 6))) - : (Listof Integer)
'(2 3 4 5 6 7)
> (queue->list (map * (queue 1 2 3 4 5 6) (queue 1 2 3 4 5 6))) - : (Listof Integer)
'(1 4 9 16 25 36)
procedure
(fold func init que1 que2 ...) → C
func : (C A B ... B -> C) init : C que1 : (Queue A) que2 : (Queue B)
fold currently does not produce correct results when the given function is non-commutative.
> (fold + 0 (queue 1 2 3 4 5 6)) - : Integer
21
> (fold * 1 (queue 1 2 3 4 5 6) (queue 1 2 3 4 5 6)) - : Integer
518400
> (define que (queue 1 2 3 4 5 6)) > (queue->list (filter (λ: ([x : Integer]) (> x 5)) que)) - : (Listof Integer)
'(6)
> (queue->list (filter (λ: ([x : Integer]) (< x 5)) que)) - : (Listof Integer)
'(1 2 3 4)
> (queue->list (filter (λ: ([x : Integer]) (<= x 5)) que)) - : (Listof Integer)
'(1 2 3 4 5)
> (queue->list (remove (λ: ([x : Integer]) (> x 5)) (queue 1 2 3 4 5 6))) - : (Listof Integer)
'(1 2 3 4 5)
> (queue->list (remove (λ: ([x : Integer]) (< x 5)) (queue 1 2 3 4 5 6))) - : (Listof Integer)
'(5 6)
> (queue->list (remove (λ: ([x : Integer]) (<= x 5)) (queue 1 2 3 4 5 6))) - : (Listof Integer)
'(6)
procedure
(andmap func que1 que2 ...) → Boolean
func : (A B ... B -> Boolean) que1 : (Queue A) que2 : (Queue B)
> (andmap even? (queue 1 2 3 4 5 6)) - : Boolean
#f
> (andmap odd? (queue 1 2 3 4 5 6)) - : Boolean
#f
> (andmap positive? (queue 1 2 3 4 5 6)) - : Boolean
#t
> (andmap negative? (queue -1 -2)) - : Boolean
#t
procedure
(ormap func que1 que2 ...) → Boolean
func : (A B ... B -> Boolean) que1 : (Queue A) que2 : (Queue B)
> (ormap even? (queue 1 2 3 4 5 6)) - : Boolean
#t
> (ormap odd? (queue 1 2 3 4 5 6)) - : Boolean
#t
> (ormap positive? (queue -1 -2 3 4 -5 6)) - : Boolean
#t
> (ormap negative? (queue 1 -2)) - : Boolean
#t
procedure
(build-queue size func) → (Queue A)
size : Natural func : (Natural -> A)
> (queue->list (build-queue 5 (λ:([x : Integer]) (add1 x)))) - : (Listof Integer)
'(1 2 3 4 5)
> (queue->list (build-queue 5 (λ:([x : Integer]) (* x x)))) - : (Listof Integer)
'(0 1 4 9 16)
> (head+tail (queue 1 2 3 4 5))
- : (Pairof
Integer
#(struct:Queue
((Rec
g294604
(U (Boxof (U (-> (Pairof Integer g294604)) (Pairof Integer g294604)))
Null))
(Listof Integer)
(Rec
g294607
(U (Boxof (U (-> (Pairof Integer g294607)) (Pairof Integer g294607)))
Null)))))
'(1 . #<Queue>)
> (head+tail (build-queue 5 (λ:([x : Integer]) (* x x))))
- : (Pairof
Integer
#(struct:Queue
((Rec
g294623
(U (Boxof (U (-> (Pairof Integer g294623)) (Pairof Integer g294623)))
Null))
(Listof Integer)
(Rec
g294626
(U (Boxof (U (-> (Pairof Integer g294626)) (Pairof Integer g294626)))
Null)))))
'(0 . #<Queue>)
> (head+tail (empty Integer)) head+tail: given queue is empty
1.6 Hood-Melville Queue
(require pfds/queue/hood-melville) | package: pfds |
Similar to real-time queues in many ways. But the implementation is much more complicated than Real-Time Queue. Uses a technique called Global Rebuilding. The data structure gives a worst case running time of O(1) for the operations head, tail and enqueue.
syntax
(Queue A)
> (queue 1 2 3 4 5 6)
- : #(struct:Queue
(Integer
(Listof Positive-Byte)
(U (Appending Positive-Byte)
(Done Positive-Byte)
(Reversing Positive-Byte)
Null)
Integer
(Listof Positive-Byte)))
#<Queue>
In the above example, the queue obtained will have 1 as its head element.
In the above example, enqueue adds the element 10 to (queue 1 2 3 4 5 6) and returns (queue 1 2 3 4 5 6 10).
In the above example, (tail (queue 1 2 3 4 5 6)), returns (queue 2 3 4 5 6).
procedure
(queue->list que) → (Queue A)
que : (Queue A)
> (queue->list (queue 10 2 34 4 15 6)) - : (Listof Positive-Byte)
'(10 2 34 4 15 6)
> (queue->list empty) - : (Listof Nothing)
'()
> (queue->list (map add1 (queue 1 2 3 4 5 6))) - : (Listof Positive-Index)
'(2 3 4 5 6 7)
> (queue->list (map * (queue 1 2 3 4 5 6) (queue 1 2 3 4 5 6))) - : (Listof Positive-Index)
'(1 4 9 16 25 36)
procedure
(fold func init que1 que2 ...) → C
func : (C A B ... B -> C) init : C que1 : (Queue A) que2 : (Queue B)
fold currently does not produce correct results when the given function is non-commutative.
> (fold + 0 (queue 1 2 3 4 5 6)) - : Integer [more precisely: Nonnegative-Integer]
21
> (fold * 1 (queue 1 2 3 4 5 6) (queue 1 2 3 4 5 6)) - : Integer [more precisely: Positive-Integer]
518400
> (define que (queue 1 2 3 4 5 6)) > (queue->list (filter (λ: ([x : Integer]) (> x 5)) que)) - : (Listof Positive-Byte)
'(6)
> (queue->list (filter (λ: ([x : Integer]) (< x 5)) que)) - : (Listof Positive-Byte)
'(1 2 3 4)
> (queue->list (filter (λ: ([x : Integer]) (<= x 5)) que)) - : (Listof Positive-Byte)
'(1 2 3 4 5)
> (queue->list (remove (λ: ([x : Integer]) (> x 5)) (queue 1 2 3 4 5 6))) - : (Listof Positive-Byte)
'(1 2 3 4 5)
> (queue->list (remove (λ: ([x : Integer]) (< x 5)) (queue 1 2 3 4 5 6))) - : (Listof Positive-Byte)
'(5 6)
> (queue->list (remove (λ: ([x : Integer]) (<= x 5)) (queue 1 2 3 4 5 6))) - : (Listof Positive-Byte)
'(6)
procedure
(andmap func que1 que2 ...) → Boolean
func : (A B ... B -> Boolean) que1 : (Queue A) que2 : (Queue B)
> (andmap even? (queue 1 2 3 4 5 6)) - : Boolean
#f
> (andmap odd? (queue 1 2 3 4 5 6)) - : Boolean
#f
> (andmap positive? (queue 1 2 3 4 5 6)) - : Boolean
#t
> (andmap negative? (queue -1 -2)) - : Boolean
#t
procedure
(ormap func que1 que2 ...) → Boolean
func : (A B ... B -> Boolean) que1 : (Queue A) que2 : (Queue B)
> (ormap even? (queue 1 2 3 4 5 6)) - : Boolean
#t
> (ormap odd? (queue 1 2 3 4 5 6)) - : Boolean
#t
> (ormap positive? (queue -1 -2 3 4 -5 6)) - : Boolean
#t
> (ormap negative? (queue 1 -2)) - : Boolean
#t
procedure
(build-queue size func) → (Queue A)
size : Natural func : (Natural -> A)
> (queue->list (build-queue 5 (λ:([x : Integer]) (add1 x)))) - : (Listof Integer)
'(1 2 3 4 5)
> (queue->list (build-queue 5 (λ:([x : Integer]) (* x x)))) - : (Listof Integer)
'(0 1 4 9 16)
> (head+tail (queue 1 2 3 4 5))
- : (Pairof
Positive-Byte
#(struct:Queue
(Integer
(Listof Positive-Byte)
(U (Appending Positive-Byte)
(Done Positive-Byte)
(Reversing Positive-Byte)
Null)
Integer
(Listof Positive-Byte))))
'(1 . #<Queue>)
> (head+tail (build-queue 5 (λ:([x : Integer]) (* x x))))
- : (Pairof
Integer
#(struct:Queue
(Integer
(Listof Integer)
(U (Appending Integer) (Done Integer) (Reversing Integer) Null)
Integer
(Listof Integer))))
'(0 . #<Queue>)
> (head+tail empty) head+tail: given queue is empty