8.14.0.1
amb: Ambiguous Operator
The amb operator.
syntax
(for/amb (for-clause ...) body-or-break ... body)
(for/amb type-ann-maybe (for-clause ...) type-ann-maybe expr ...+)
syntax
(for*/amb (for-clause ...) body-or-break ... body)
(for*/amb type-ann-maybe (for-clause ...) type-ann-maybe expr ...+)
Iterate like for/list and for*/list respectively, they allow
programmers to explore different possibilities in a non-deterministic way.
Examples:
> (parameterize ([current-amb-queue (make-queue)]) (let ([x (for/amb ([i (in-range 10)]) (displayln i) i)]) (when (< x 5) (amb))))
0
1
2
3
4
5
> (with-handlers ([exn:fail:contract? void]) (parameterize ([current-amb-queue (make-queue)]) (let ([x (for/amb ([i (in-range 10)]) i)]) (when (even? x) (amb)) (displayln x) (amb))))
1
3
5
7
9
> (parameterize ([current-amb-queue (make-queue)]) (let-values ([(x y) (for/amb ([v (in-list '([2 9] [9 2]))]) (apply values v))]) (when (> x y) (amb)) (displayln (list x y)))) (2 9)
Inserts new amb nodes for all alternatives in alt*
into the current amb queue. An amb node is a thunk
that calls k with the values produced by an alternative.
parameter
(current-amb-shuffler) → (-> list? list?)
(current-amb-shuffler amb-shuffler) → void? amb-shuffler : (-> list? list?)
A parameter that determines how to shuffle alt*
before inserting new amb nodes into the amb queue. The default
value is reverse.
parameter
(current-amb-queue amb-queue) → void? amb-queue : queue?
A parameter that holds the queue of amb nodes to be evaluated.
The queue is initially empty and is populated by insert-amb-node*!.
parameter
(current-amb-dequeue!) → (-> queue? (-> none/c))
(current-amb-dequeue! amb-dequeue!) → void? amb-dequeue! : (-> queue? (-> none/c))
A parameter that determines how to dequeue an amb node from
the queue. The default value is dequeue!, which means the node at the
front of the queue is removed and returned.
A parameter that determines how to enqueue an amb node into
the queue. The default value is enqueue-front!, which means the node is
added to the front of the queue.