[Challenge] implementing the ambiguous operator in D

Philippe Sigaud philippe.sigaud at gmail.com
Fri Sep 3 12:29:50 PDT 2010


On Fri, Sep 3, 2010 at 19:51, Peter Alexander
<peter.alexander.au at gmail.com>wrote:

> == Quote from Simen kjaeraas (simen.kjaras at gmail.com)'s article
> > Yes, very much so. However, Peter Alexander has misunderstood the
> > ambiguous operator.
>
> Hey, I was just going by what the guy posted :)  He mentioned
> nothing of tuples, and his examples certainly didn't show any
> tuples.
>

Yes, I agree with Peter there. As I said, I personally prefer the examples
in the SO OP than the Haskell/Ruby code, if only because the example are
easily understood and I find this range combinator useful in some
circumstances.
What we have here in D is a bit like the List monad in Haskell: a way to
represent a bunch of possible values for a computation. If a can 2 or 4 or
6, and b can be 0 or 1 or 2, it makes sense for a*b to among 0,2,4,6,8,12.

So thanks, Peter. But does the code compile for you? As I said, here with
2.048, it doesn't.

Now, the 'real'/intriguing/mind-bending amb operator (from the 60's) does
like the Haskell implementation linked in SO does: backtracking on the
results to avoid some condition. If someone is up to the challenge of
implementing it in D, great! Maybe with closures? I never really thought
about it.

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. Maybe in this case an
'alias possibilities[0] this' could do the trick:

assert(x == 2);
assert(y == 4);

Can we do alias someArray[0] this; ?


Philippe




Philippe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20100903/1cca2160/attachment.html>


More information about the Digitalmars-d mailing list