[Challenge] implementing the ambiguous operator in D
Andrei Alexandrescu
SeeWebsiteForEmail at erdani.org
Sun Sep 5 06:56:59 PDT 2010
On 09/05/2010 07:59 AM, Philippe Sigaud wrote:
>
>
> On Sun, Sep 5, 2010 at 12:00, Simen kjaeraas <simen.kjaras at gmail.com
> <mailto:simen.kjaras at gmail.com>> wrote:
>
> Philippe Sigaud <philippe.sigaud at gmail.com
> <mailto:philippe.sigaud at gmail.com>> wrote:
>
> I guess the D syntax would be
> auto x = amb([1,2,3]);
> auto y =amb([4,5,6]);
> x*y == 8; // forces desambiguation => the ambs explore the
> possibilities.
> assert(x == amb([2]));
> assert(y == amb([4]));
>
> There is only one value left, no more ambiguity.
>
>
> But what happens in the case where there is more than one value left?
>
>
> If I understand correctly the linked Ruby and Haskell versions, the
> search stops as soon as you find a possibility. But then, I'm no pro on
> this.
>
>
> That's one of the reasons I feel that doing the combination, then
> filtering it, is more correct. There is also the fact that the above
> syntax does not let you call arbitrary functions in the requirements,
> something filter does.
>
>
> The combining and filtering is interesting (in this case, it'd be like
> doing a list comprehension).
> But the real challenge in the SO question is the 'going back in time'
> part, which I have trouble to understand : how can you modify x and y
> through a multiplication and a comparison?
>
> I did a range comprehension maybe one year ago, which would be partially
> equivalent to the problem:
>
> auto solution = comp!("[a,b]", "a*b == 8")([1,2,3], [4,5,6]);
>
> solution is a range, in this case a one element range, containing only
> [2,4].
Here we need to have a crossProduct range, as we discussed in the thread
"Combining infinite ranges". Then it's easy to combine it with filter:
auto solutions = filter!"a*b == 8"(crossProduct([1,2,3], [4,5,6]));
Andrei
More information about the Digitalmars-d
mailing list