[racket] handy trick for "amb-collect" desired

From: Thomas Chust (chust at web.de)
Date: Tue Sep 14 11:50:31 EDT 2010

2010/9/14 Stefan Busch <stefan_busch_ at arcor.de>:
> [...]
> Your solution looked convincing to me,
> but when I try e.g.
> (amb list (list 1 2 3 4))
> it's not evaluated to the desired
> 1 2 3 4,
> but to
> 1.
> [...]


I assume you meant to say (amb-list ...) instead of (amb list ...). Of course
this expression evaluates to the first item in the list. This is the whole
point of amb: To return the result *one* of its arguments evaluates to, but to
provide the possibility of backtracking and choosing another argument.

If you want a list of all possible return values of an expression, you *must*
wrap it in an amb-collect. The implementation of amb provided by
(planet murphy/amb:1:1/amb) will even signal an error if the dynamic context
of an ambiguous expression is not enclosed by something equivalent to amb-find
or amb-collect.


When C++ is your hammer, every problem looks like your thumb.

Posted on the users mailing list.