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

From: Will M. Farr (wmfarr at gmail.com)
Date: Sun Sep 12 14:16:44 EDT 2010


amb is a macro that chooses among the results of evaluating the expressions it is provided, so 

(amb *someList*)

evaluates *someList*, resulting in a list, and chooses that.  You could define a function 

(define (amb-list list)
  (if (null? list)
      (amb (car list)
           (amb-list (cdr list)))))

It's also possible that the amb library provides such a function, which may be implemented more efficiently; I don't know which amb library you're using, so I can't help you there.

Note that the simple solution of using 

(apply amb *someList*) 

will fail, since amb is a macro, not an ordinary function.  Good luck!


On Sep 12, 2010, at 9:16 AM, Stefan Busch wrote:

> Hello racket community,
> though it may be impolite to enter the community with a wish,
> I'd like to ask for help with the following problem:
> I want a and b to "run through" all elements of two given lists,
> and then to catch the combinations that fullfill a desired condition with
> an "amb-assert" statement.
> If I try this in the following way
> (amb-collect
> (let ((a (amb *someList*)
>        (b (amb *someDifferentList*))...............
> it turns out that not the list elemets are considered possible values for a or b,
> but the lists themselves are treated as the only possible value.
> In all examples I could find for the uage of "amb-collect" the values to run through were
> recited, but in my case these are quite many, so would be grateful for a handier solution.
> thanks,
> Stefan
> _________________________________________________
>  For list-related administrative tasks:
>  http://lists.racket-lang.org/listinfo/users

Posted on the users mailing list.