皆さん、私はC++から次のコードを持っています。
for (int i=0; i < nObstacles; i++)
{
int x,y;
bool bAlreadyExists;
do {
x = rand() % nGridWidth;
y = rand() % nGridHeight;
} while (HasObstacle(x, y));
SetObstacle(x, y, true);
}
問題なく直接F#に変換できます。
let R = new System.Random()
for i=0 to nObstacles do
let mutable bGoodToGo = false;
let mutable x =0;
let mutable y = 0
while not bGoodToGo do
x <-R.Next(nWidth)
y <-R.Next(nHeight)
bGoodToGo <- IsEmptyAt x y
board.[x,y]<-Obstacle;
もちろん、これはF#の使用方法ではないため、ほとんどの人がうんざりするでしょう。このコードには、do-whileループや可変データなど、F#の「アンコッシャー」の概念がいくつかあります。
しかし、私が興味を持っているのは、不変のデータを使用した「適切な」F#変換であり、ある種のdo-whileと同等です。