2011-11-14 38 views
5

Ho codice come questo in C#:Conversione C# struct a F #

namespace WumpusWorld 
    { 
     class PlayerAI 
     { 
      //struct that simulates a cell in the AI replication of World Grid 
      struct cellCharacteristics 
      { 
       public int pitPercentage; 
       public int wumpusPercentage; 
       public bool isPit; 
       public bool neighborsMarked; 
       public int numTimesvisited; 
      } 

      private cellCharacteristics[,] AIGrid;     //array that simulates World Grid for the AI 
      private enum Move { Up, Down, Right, Left, Enter, Escape }; //enum that represents integers that trigger movement in WumpusWorldForm class 
      Stack<int> returnPath;          //keeps track of each move of AI to trace its path back 
      bool returntoBeg;           //flag that is triggered when AI finds gold 
      int numRandomMoves;           //keeps track of the number of random moves that are done 

      public PlayerAI() 
      { 
       AIGrid = new cellCharacteristics[5, 5]; 
       cellCharacteristics c; 
       returntoBeg = false; 
       returnPath = new Stack<int>(); 
       numRandomMoves = 0; 

       for (int y = 0; y < 5; y++) 
       { 
        for (int x = 0; x < 5; x++) 
        { 
         c = new cellCharacteristics(); 
         c.isPit = false; 
         c.neighborsMarked = false; 
         c.numTimesvisited = 0; 

         AIGrid[x, y] = c; 
        } 
       } 
      } 
     } 
    } 

non so come convertire questo C# struct a F # e attuare la struct in un array come il mio codice di cui sopra.

+1

Perché non usi solo la struttura C# da F #? Con cosa hai esattamente problemi? Che cosa hai provato? A proposito, le strutture mutabili sono malvagie e non dovresti usarle a meno che non sia davvero necessario. – svick

+0

è solo un piccolo pezzo del mio codice, che struct è all'interno di una classe, e la classe ha metodi che usano la struct come Array. Provo a convertire la mia classe C# in classe F #. Aggiungo altro codice nel mio codice di esempio –

risposta

10

È possibile definire una struttura utilizzando la parola chiave struct (come mostrato da Stilgar) o utilizzando l'attributo Struct, che è simile a questo. Ho anche aggiunto un costruttore che inizializza la struttura:

[<Struct>] 
type CellCharacteristics = 
    val mutable p : int 
    val mutable w : int 
    val mutable i : bool 
    val mutable ne : bool 
    val mutable nu : int 

    new(_p,_w,_i,_ne,_nu) = 
    { p = _p; w = _w; i = _i; ne = _ne; nu = _nu } 

// This is how you create an array of structures and mutate it 
let arr = [| CellCharacteristics(1,2,true,false,3) |] 
arr.[0].nu <- 42 // Fields are public by default 

Tuttavia, non è generalmente consigliabile utilizzare tipi di valore mutabili. Ciò causa un comportamento confuso e il codice è abbastanza difficile da ragionare. Questo è scoraggiato non solo in F #, ma anche in C# e .NET in generale. In F #, la creazione di una struct immutabile, è anche più facile:

// The fields of the strcuct are specified as part of the constructor 
// and are stored in automatically created (private) fileds 
[<Struct>] 
type CellCharacteristics(p:int, w:int, i:bool, ne:bool, nu:int) = 
    // Expose fields as properties with a getter 
    member x.P = p 
    member x.W = w 
    member x.I = i 
    member x.Ne = ne 
    member x.Nu = nu 

Quando si utilizzano le strutture immutabili, non sarà in grado di modificare i singoli campi della struct. Dovrai sostituire l'intero valore della struttura nell'array. In genere è possibile implementare il calcolo come membro della struct (ad esempio Foo) e quindi scrivere semplicemente arr.[0] <- arr.[0].Foo() per eseguire l'aggiornamento.

+0

in qualche modo capisco del tuo codice, ma non riesco ancora ad ottenerlo quando voglio implementarlo nel mio codice. Nel mio codice, la struct è in una classe, e in C# posso implementare facilmente la struct in una matrice. Non posso spiegarlo meglio, perché il mio inglese non va bene. Se non ti dispiace, dai un'occhiata a questo link: http://stackoverflow.com/questions/8126680/need-help-to-convert-my-homework-from-c-sharp-to-f –

+0

oh sorry , sembra che ho infranto le regole, quindi il link è chiuso –