[plt-scheme] Swindle/amb-collect
On Aug 13, michael rice wrote:
> Can anyone give me a *simple* example of the usage of
> (amb-collect expr). Docs say "evaluate expr, using
> amb-fail repeatedly until all options exhausted &
> returns list of results."
Here's a function that returns either 1, 2, or 3:
| => (define (foo) (amb 1 2 3))
Each time you invoke it it returns one of these, which without any
other constraints will always be 1.
| => (foo)
| 1
To get the second result, you need to simulate a failure using `amb'
which is an error (since there are no expressions to choose from):
| => (amb)
| 2
| => (amb)
| 3
| => (amb)
| amb: tree exhausted
`amb-collect' is used to get a list of these results:
| => (amb-collect (foo))
| (1 2 3)
which can be used with any expression
| => (amb-collect (* 2 (foo)))
| (2 4 6)
and with multiple occurences of ambiguous computations:
| => (amb-collect (list (foo) (foo)))
| ((1 1) (1 2) (1 3) (2 1) (2 2) (2 3) (3 1) (3 2) (3 3))
| => (amb-collect (* (amb 1 2) (foo)))
| (1 2 3 2 4 6)
--
((lambda (x) (x x)) (lambda (x) (x x))) Eli Barzilay:
http://www.barzilay.org/ Maze is Life!