Non-deterministic selection using the amp operator

Is it possible to implement a McCarthy amboperator for non-deterministic selection in C #?

Apparently, .NET does not have continuation support, but it yield returncan be useful. Is this possible in other static .NET languages ​​like F #?

+5
source share
2 answers

Yes, it yield returnexecutes the continuation form. Although for many useful cases, Linq provides functional operators that allow you to combine a lazy sequence generator, so in fact in C # 3 there is no need to use yield returnso much (except for adding additional Linq extensions in yours to fill in the spaces in the library, for example Zip, Unfold) .

In the example, we multiply an integer by brute force. Essentially, the same example in C # can be performed with Linq built-in operators:

var factors = Enumerable.Range(2, 100)
        .Join(Enumerable.Range(2, 100), 
              n => 1, n => 1, (i, j) => new { i, j })
        .First(v => v.i*v.j == 481);

Console.WriteLine("Factors are " + factors.i + ", " + factors.j);

Here, the starting points are my two calls Enumerable.Range, which is built into Linq, but you can implement yourself as:

IEnumerable<int> Range(int start, int stop)
{
    for (int n = start; n < stop; n++)
        yield return n;
}

, n => 1, n => 1 Join. 1 Join , , .

( ):

(i, j) => new { i, j })

, , :

.First(v => v.i*v.j == 481);

First . , "", :

.First(v => 
       {
           Console.WriteLine("Aren't lambdas powerful things?");

           return v.i*v.j == 481;
       );

, , , . , , false - amb .

+5

, , .

amb . , , , Prolog - , ( , ).

#, YieldProlog. , .

http://yieldprolog.sourceforge.net/

+5

All Articles