我想下面的C#轉換爲F#:在類型使用F#索引屬性
public class Matrix
{
double[,] matrix;
public int Cols
{
get
{
return this.matrix.GetUpperBound(1) + 1;
}
}
public int Rows
{
get
{
return this.matrix.GetUpperBound(0) + 1;
}
}
public Matrix(double[,] sourceMatrix)
{
this.matrix = new double[sourceMatrix.GetUpperBound(0) + 1, sourceMatrix.GetUpperBound(1) + 1];
for (int r = 0; r < this.Rows; r++)
{
for (int c = 0; c < this.Cols; c++)
{
this[r, c] = sourceMatrix[r, c];
}
}
}
public double this[int row, int col]
{
get
{
return this.matrix[row, col];
}
set
{
this.matrix[row, col] = value;
}
}
}
這是我到目前爲止有:以上
type Matrix(sourceMatrix:double[,]) =
let mutable (matrix:double[,]) = Array2D.create (sourceMatrix.GetUpperBound(0) + 1) (sourceMatrix.GetUpperBound(1) + 1) 0.0
member this.Item
with get(x, y) = matrix.[(x, y)]
and set(x, y) value = matrix.[(x, y)] <- value
do
for i = 0 to matrix.[i].Length - 1 do
for j = (i + 1) to matrix.[j].Length - 1 do
this.[i].[j] = matrix.[i].[j]
我的類型似乎有兩個問題我不知道如何解決。第一個是矩陣。[(x,y)]預計有類型`a []但是類型爲double [,]。第二種是類型定義在成員和接口定義之前必須有let/do綁定。這個問題是我試圖填充do塊中的索引屬性,這意味着我必須先創建它。
由於提前,
鮑勃
對不起,我只是編輯它,粘貼在錯誤的構造函數中。 – Beaker 2011-03-05 21:48:20
優秀的答案,以及只讀的這是你的一個正確的假設。原來的C#不是我的,我忽略了這一點。對C#和F#都很好。 :) – Beaker 2011-03-05 23:35:46
如果有人感興趣,我添加了這個問題的後續問題:http://stackoverflow.com/questions/5212570/adding-overloaded-constructors-to-implicit-f-type – Beaker 2011-03-06 19:07:13