[racket] handy trick for "amb-collect" desired
2010/9/14 Stefan Busch <stefan_busch_ at arcor.de>:
> [...]
> The teaching material I got from university gives the following exanple for
> the usage
> of amb-collect:
>
> ***************************************************************************************
>
> ;;get all pairs of values von a and b,
> ;;which have sum 7
>
> (amb-collect)
> ( let ( ( a (amb 1 2 3 4 5 6 7 ) )
> (b (amb 2 4 6 8 ) ) )
> ( amb-assert (= (+ a b) 7 ) )
> ( cons a b ) ) )
>
>
> -------->( ( 1 . 6) (3 . 4) (5 . 2 ) )
>
> ***************************************************************************************
> [...]
Hello Stefan,
this example has a spelling mistake: The closing parenthesis directly after
amb-collect is wrong, because the dynamic scope of the amb expressions
must be enclosed by that of the amb-collect expression. But apart from that
it looks fine. If I take the example and change the calls of amb into uses of
amb-list as described in earlier messages in this thread, everything still
works fine:
#lang racket
(require
(planet murphy/amb:1:1/amb))
(define (amb-list values)
(apply amb-call (map const values)))
(define a-candidates
(list 1 2 3 4 5 6 7))
(define b-candidates
(list 2 4 6 8))
(amb-collect
(let ([a (amb-list a-candidates)]
[b (amb-list b-candidates)])
(amb-assert (= (+ a b) 7))
(cons a b)))
I really don't see where there may be a problem.
Ciao,
Thomas
--
When C++ is your hammer, every problem looks like your thumb.