Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6810877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:15:33+00:00 2026-05-26T20:15:33+00:00

I have code like this in C#: namespace WumpusWorld { class PlayerAI { //struct

  • 0

I have code like this in C#:

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;
                    }
                }
            }
        }
    }

I don’t know how to convert this C# struct to F# and implement the struct into an array like my code above.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:15:34+00:00Added an answer on May 26, 2026 at 8:15 pm

    You can define a struct either using the struct keyword (as shown by Stilgar) or using the Struct attribute, which looks like this. I also added a constructor that initializes the structure:

    [<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
    

    However, it is generally not recommended to use mutable value types. This causes confusing behavior and the code is quite difficult to reason about. This is discouraged not just in F#, but even in C# and .NET in general. In F#, creating an immutable struct is also easier:

    // 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
    

    When using immutable structs, you won’t be able to modify individual fields of the struct. You’ll need to replace the entire struct value in the array instead. You can typically implement calculation as a member of the struct (say Foo) and then just write arr.[0] <- arr.[0].Foo() to perform the update.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data structure that represents C# code like this: class Namespace: string
I have code like this: namespace N { class B { public: virtual void
I have a model class like this: namespace Models { public struct Localization {
I have code like this: template <typename T, typename U> struct MyStruct { T
Assume I have code like this: var svc = new Namespace.SvcClient(); var request =
Say I have some code like namespace Portal { public class Author { public
I have code like this: var newMsg = new Msg { Var1 = var1,
I have code like this to move the player in my game left, right,
I have code like this var MyObj = { f1 : function(o){ o.onmousedown =
I have code like this: ... entry.KeyPressEvent += EntryKeyPressEvent; ... } void EntryKeyPressEvent(object o,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.