Problems when reimplementing c# class to f#
-
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 usemember 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 :(. -
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 usemember 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 :(.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