Limboでエラトステネスのふるい
IPWLから。スレッドとチャネルを使った並行版エラトステネスのふるい。initのwhileループが無限整数列を作るジェネレータになっていて、各スレッドがふるいになっている。
implement Eratosthenes;
include "sys.m";
include "draw.m";
sys : Sys;
Eratosthenes : module
{
init : fn(nil : ref Draw->Context, nil : list of string);
};
init(nil : ref Draw->Context, nil : list of string)
{
sys = load Sys Sys->PATH;
i := 2;
sourcechan := chan of int;
spawn sieve(i, sourcechan);
while () {
sourcechan <-= i++;
}
}
sieve(ourprime : int, inchan : chan of int)
{
n : int;
sys->print("%d ", ourprime);
newchan := chan of int;
while (!((n = <- inchan) % ourprime)) {
}
spawn sieve(n, newchan);
while () {
if ((n = <- inchan) % ourprime) {
newchan <-= n;
}
}
}おそらくErlangとかでもspawnとメールボックスを使って、同じプログラムを書けるのでは?Erlangのメッセージパッシングは非同期だけど。