1

d 言語でアクター モデルを使用して階乗関数を実装しようとしています。私の目的は、アクターを作成して各パーツを単独で計算し、新しいアクターを生成して次のアクターを作成することです。私は D の初心者なので、言語の使い方を学んでいます。私の目的は、階乗の実装をさらに拡張することです。これは単なるテストです。

これが私の問題です。再帰を使用して階乗を実装しようとしています。ファクト関数は、基本条件に達していない限り、行の次の番号の新しいファクト スレッドを作成します。

私のコード:

void fact(Tid tid)
{

        int fact = 1;
        receive
        (
        (int i)
        {
            if(i == 0)
            {
                writeln("End of recursion");
            }
            else
            {
                fact *= i;
                send(thisTid, i-1);
            }
        }
    );
    send(tid, fact);
}

void main()
{

        auto tid = spawn(&fact, thisTid);
        int x = 3;
        send(tid, x);
        auto fact = receiveOnly!(int);
        writeln(fact);
}

それが可能かどうかさえわかりませんが、いずれにせようまくいきません。spwn in fact 関数を追加しようとすると、次のエラーが返されます。

src/main.d(62): Error: template std.concurrency.spawn does not match any function template declaration
/usr/include/x86_64-linux-gnu/dmd/phobos/std/concurrency.d(399): Error: template std.concurrency.spawn(T...) cannot deduce template function from argument types !()(int*,Tid)
src/main.d(63): Error: template std.concurrency.send does not match any function template declaration
/usr/include/x86_64-linux-gnu/dmd/phobos/std/concurrency.d(463): Error: template std.concurrency.send(T...) cannot deduce template function from argument types !()(_error_,int)

それで、私がやろうとしていることをすることは可能ですか?どのように?そうでない場合、そのようなことを可能にする計画はありますか?

助けてください。

4

2 に答える 2

4

factここではベースから外れている可能性がありますが、そのエラーメッセージから、 への呼び出しで、dmd が使用していると思われる を使用していないように見えますspawn。という名前の整数がいくつかfactありますが、この例では(400行ではないため、明らかに削減されています)、それらのいずれも競合しませんが、完全なコードでは、そのうちの1つが競合&factする可能int*factがありintます(

関数の名前を階乗などに変更し、spawn必要に応じて呼び出しを変更してみてください。整数を変更しないようにしてください。

于 2012-05-22T04:34:37.753 に答える
2

私にとってはうまくいきます。使用している DMD のバージョンは何ですか? まだ 2.059 にアップグレードしていない場合は、2.059 にアップグレードしてみてください。

(注:コンパイルして実行するという点で機能factすると言います。返される前に1つの数字しか返されないため、書き込み応答は返されないため、3つだけが返されます。ループ内にあるreceive必要があります)receive

admin@poita ~% cat test.d
import std.stdio;
import std.concurrency;

void fact(Tid tid)
{
    int fact = 1;
    receive
    (
        (int i)
        {
            if(i == 0)
            {
                writeln("End of recursion");
            }
            else
            {
                fact *= i;
                send(thisTid, i-1);
            }
        }
    );
    send(tid, fact);
}

void main()
{
    auto tid = spawn(&fact, thisTid);
    int x = 3;
    send(tid, x);
    auto fact = receiveOnly!(int);
    writeln(fact);
}

admin@poita ~% dmd test.d
admin@poita ~% ./test
3
admin@poita ~%
于 2012-05-19T11:56:51.340 に答える