Search code examples
c#.netf#nemerle

F# vs C# vs Nemerle


Got new project in my TODO and can't chose F# or Nemerle.

I'm currently learning F# and have some projects on Nemerle.

I like F# way , I like indent by default (also I want indent by default for nemerle2), I like many features and magic of F# but there is no macros.

The goal of F# is VS2010 and maybe (maybe) bigger developers team and it's looking like Haskell(can create light Linux programs with it and it's fun).

The goal of Nemerle is macros and I think I like some syntax of Nemerle more.

and most people just like C#...

  • So the language must be perspective
  • Syntax must be logical , easy to understand, neat and powerfull(less code)
  • It must be easy to use with .NET
  • So I already closed my self with the chose of this 3 ones.
  • I can use them all but what about chose one. It's not so easy for me.

just for example I like (Nemerle)

match(STT)
    | 1 with st= "Summ"
    | 2 with st= "AVG" =>
        $"$st : $(summbycol(counter,STT))"

much more then (F#)

let (|Let|) v e = (v, e)

match stt with 
| Let "Summ" (st, 1) 
| Let "AVG" (st, 2) -> srintf "%s ..." st

F# :

["A"; "B"] |> List.iter (fun s  -> printfn "%d"  s.Length)

Nemerle:

["A", "B"].Iter(x => printf("%d", x.Length))

F# (hope not mistaken here):

let type X =
    let mytable a = ""
    let mytable b = ""
    new(A, B) = {
    a <- A
    b <- B }
    member X.A 
        with get  = a
    member X.B 
        with get  = a

Nemerle :

[Record]
class X
  public A : string { get; }
  public B : string { get; }

C# :

class X
{
    private readonly string _a;
    public string A { get { return _a; } }

    private readonly string _b;
    public string B { get { return _b; } }

    public X(string a, string b)
    {
        _a = a;
        _b = b;
    }
}

and here is nemerle code I already can't convert to F# (so I only learning it) ...

abstract class ABase
    abstract public A : string { get; }

interface IB
    B : string { get; }

[Record]
class My : ABase, IB
    public override A : string { get; }
    public virtual  B : string { get; }

Comparison to C#:

abstract class ABase
{
    abstract public string A { get; }
}

interface IB
{
    string B { get; }
}

class My : ABase, IB
{
    private readonly string _a;
    public override A : string { get { return _a; } }

    private readonly string _b;
    public virtual  B : string { get { return _b; } }

    public My(string a, string b)
    {
        _a = a;
        _b = b;
    }
}

Clearly Nemerle code is easier to support and more readable.

@Brian So that's why I'm asking , show me if that real to make this easer on F# , C# also if you see I do it wrong because I'm not sure about other ways to make clearly the same.


Solution

  • F# and Nemerle versions quite different:

    1. Nemerle defines class while F# defines struct.
    2. Nemerle defines readonly members while F# defines mutable members.
    3. Nemerle defines public properties while F# defines public fields.
    4. Nemerle defines constructor while F# doesn't define it.

    Analogous Nemerle code for F# example is following:

    struct X
      mutable A : string
      mutable B : string
    

    The second example is almost the same:

    1. Nemerle defines constructor for My while F# doesn't define it.
    2. Nemerle defines Properties which return the value passed in constructor while F# defines properties with constant values.

    Nemerle version is much shorter and clearer than F# version here.

    P.S. About curly braces vs indent syntax. Nemerle supports both syntaxes.

    You can write either:

    class M
    {
      static Main() : void
      {
        Console.WriteLine("A");
      }
    }
    

    Or use indent:

    #pragma indent
    class M
      static Main() : void
          Console.WriteLine("A");
    

    Or even use both styles !

    #pragma indent
    class M
      static Main() : void { Console.WriteLine("A"); }