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

From: Stefan Busch (stefan_busch_ at arcor.de)
Date: Tue Sep 14 10:27:16 EDT 2010

Hello Will,

thanks for your suggestion.
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 think the problem is that the recursion doesn't work,
because if in

(amb (car list)

            (amb-list (cdr list)))))


the first line doesnt' lead to a fail,
then the second line isn't evaluated.

greetings,
Stefan





Am 12.09.2010 20:16, schrieb Will M. Farr:
> Stefan,
>
> 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)
>        (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!
>
> Will
>
> 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.