Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Problems when reimplementing c# class to f#

Problems when reimplementing c# class to f#

Scheduled Pinned Locked Moved .NET (Core and Framework)
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Ravadre
    wrote on last edited by
    #1

    Hello, Recently I've decided to rewrite some of my C# code to F# (pratice, learn, maybe move and optimize some code for future). Unfortunately I've faced few problems. Let's say I have a c# class that looks like this:

    public class MersenneTwister : Random
    {
    private const int N = 624;
    private uint[] mt = null;

    public MersenneTwister(uint seed)
    {
        mt = new uint\[N\]
        mt\[0\] = seed & 0xffffffffU;
        for (int mti = 1; mti < N; ++mti)
        {
             mt\[mti\] = (69069 \* mt\[mti - 1\]) & 0xffffffffU;
        }
    }
    

    }

    I have no idea how to create constants like N - I could write static let N = 24 but: - After compilation, c#'s version does replace N with number, as consts should work, f# does not (it uses static field to represent N) - I can't use static let if I don't have implicit constructor for type, and I can't have one, because my constructor has to implement some code. I have to create mt array as mutable, but I can't use let (no implicit constructor), if I use member x.mt : uint32 array = Array.create N 0u instead, new array will be created each time I use mt property. I will be grateful for any ideas. I also must add that, I can't believe so many people say f# is much more expressive and easier then c#, I'm trying to learn it, but for now, each time I want to do something my way, I have to search informations in books and/or google a lot :(.

    K 1 Reply Last reply
    0
    • R Ravadre

      Hello, Recently I've decided to rewrite some of my C# code to F# (pratice, learn, maybe move and optimize some code for future). Unfortunately I've faced few problems. Let's say I have a c# class that looks like this:

      public class MersenneTwister : Random
      {
      private const int N = 624;
      private uint[] mt = null;

      public MersenneTwister(uint seed)
      {
          mt = new uint\[N\]
          mt\[0\] = seed & 0xffffffffU;
          for (int mti = 1; mti < N; ++mti)
          {
               mt\[mti\] = (69069 \* mt\[mti - 1\]) & 0xffffffffU;
          }
      }
      

      }

      I have no idea how to create constants like N - I could write static let N = 24 but: - After compilation, c#'s version does replace N with number, as consts should work, f# does not (it uses static field to represent N) - I can't use static let if I don't have implicit constructor for type, and I can't have one, because my constructor has to implement some code. I have to create mt array as mutable, but I can't use let (no implicit constructor), if I use member x.mt : uint32 array = Array.create N 0u instead, new array will be created each time I use mt property. I will be grateful for any ideas. I also must add that, I can't believe so many people say f# is much more expressive and easier then c#, I'm trying to learn it, but for now, each time I want to do something my way, I have to search informations in books and/or google a lot :(.

      K Offline
      K Offline
      Kevin McFarlane
      wrote on last edited by
      #2

      Ravadre wrote:

      I can't believe so many people say f# is much more expressive and easier then c#

      It's more expressive but it is certainly not easier.

      Kevin

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups