简体   繁体   English

Nemerle和F#的比较功能On .Net

[英]Comparison Of Nemerle and F# For Functional On .Net

Community Wiki Question: 社区维基问题:

Pursuant to this question: What are the benefits of using Scala in .Net? 根据这个问题: 在.Net中使用Scala有什么好处? another question comes to mind. 另一个问题浮现在脑海中。 Can anyone lay out the comparative advantages (and disadvantages) of Nemerle and F# for functional development on the .Net platform? 任何人都可以在.Net平台上列出Nemerle和F#的功能开发的比较优势(和劣势)吗? I've just looked at Nemerle in passing. 我刚刚看过Nemerle。 It sounds like it kind of plays in the same ballpark as F# so I was wondering what differences there are other than the obvious syntax differences and the big advantage F# has of being backed by Microsoft. 听起来它和F#在同一个场地玩得很好,所以我想知道除了显而易见的语法差异和F#支持微软的巨大优势之外还有什么区别。

I've touched both these languages and my impressions on Nemerle are briefly the following:(I assume that most of the audience is familiar with F# and Nemerle is less popular so for the sake of fairness I'll cover it a bit more): 我已触及这两种语言,我对Nemerle的印象简要如下:(我认为大多数观众都熟悉F#而Nemerle不太受欢迎,所以为了公平起见,我会更多地介绍一下):

  • F# community is rather big and it grows constantly due to large number of blogposts, articles etc. Also it is spreaded across the countries. F#社区相当大,并且由于大量的博客文章,文章等而不断增长。此外,它遍布各个国家。 As the opposite, Nemerle enthusiasts are basically russian-speaking and concentrated on RSDN.ru site. 相反,Nemerle爱好者基本上讲俄语并且集中在RSDN.ru网站上。
  • Nemerle syntax is IMO much friendlier for developers with background in C-like languages. 对于具有类C语言背景的开发人员来说,Nemerle语法对于IMO来说非常友好。
  • Nemerle (as well as F#) has type inference features. Nemerle(以及F#)具有类型推断功能。 Type inference mechanism in Nemerle is bound to method body (local functions, variables and so on), opposing to F# global type inference scope. Nemerle中的类型推断机制绑定到方法体(局部函数,变量等),与F#全局类型推理范围相对。 However Nemerle compiler doesn't enforce any specific idioms of writing code to assist type inference mechanism. 但是,Nemerle编译器不强制执行编写代码以帮助类型推断机制的任何特定习惯用法。

F# F#

open System.Text

let l = [1; 2; 3]
let r1 = l |> List.fold(fun (sb : StringBuilder) v -> sb.Append(v).AppendLine()) (StringBuilder()) // type annotation is required on the function argument
let r2 = (StringBuilder(), l) ||> List.fold(fun sb v -> sb.Append(v).AppendLine()) //here compiler can infer type of State parameter 

Nemerle Nemerle

using System.Console; 
using System.Collections.Generic; 
using System.Text; 

def l = [1,2,3]; 
def res = l.FoldLeft(StringBuilder(), (v, acc) => acc.Append(v).AppendLine()); 
WriteLine($"Result:\n$res"); 

def d = Dictionary(); // generic parameters are absent (even placeholders!!!) 
d.Add(1, "!"); 
WriteLine(d.GetType()); // System.Collections.Generic.Dictionary`2[System.Int32,System.String] 

Also you may notice another feature of Nemerle compiler – it can infer types from further usage. 您还可能注意到Nemerle编译器的另一个功能 - 它可以从进一步的使用中推断出类型。 To deduce types F# uses approach based on Hindley-Milner algorithm and tries to infer most generic type. 推导类型F#使用基于Hindley-Milner算法的方法,并尝试推断出最通用的类​​型。 Nemerle, in opposite, never infers polymorphic types and always looks for most specific type. 相反,Nemerle从不推断多态类型,总是寻找最具体的类型。

F# F#

let addInt = (+) 5
let addString = (+) "!!!"

let run f x = f (f x) // ('T -> 'T) -> 'T -> 'T

run addInt 5
run addString "S"

Nemerle in the same conditions will infer type of run as (int->int) * int -> int. 在相同条件下的Nemerle将推断运行类型为(int-> int)* int - > int。

More details on Nemerle type inference mechanism can be found in MSc thesis of Michal Moskal: Type Inference With Deferral 关于Nemerle类型推理机制的更多细节可以在Michal Moskal的MSc论文中找到: 推断类型推断

  • Nemerle has rich metaprogramming capabilities. Nemerle具有丰富的元编程功能。 Most of language control constructs, like loops, conditional expressions, LINQ support, forthcoming feature of parsing included C# sources and many more – all of them are created using macros. 大多数语言控制构造,如循环,条件表达式,LINQ支持,即将到来的解析功能包括C#源等等 - 所有这些都是使用宏创建的。 One sample of macros applications can be found here . 可在此处找到一个宏应用程序示例。 BTW, string formatting capabilities with $ syntax in the sample above - is also the built-in macro. BTW,上面示例中带有$ syntax的字符串格式化功能 - 也是内置宏。

EDIT: Added slightly larger sample 编辑:添加稍大的样本

using System.Console;
using System.Collections.Generic;
using System.Text;

variant Expr
{
  | Const { value : double }
  | Var { name : string }
  | Operation { id : string; left : Expr; right : Expr }

  public Eval(operations : Dictionary[string, double*double -> double], context : Dictionary[string, double]) : double
  {
    match(this)
    {
        | Const (value) => value
        | Var(name) => context[name]
        | Operation(id, left, right) => 
            def f = operations[id];
            f(left.Eval(operations, context), right.Eval(operations, context))
    }
  }
}

module Program
{
    public Main() : void
    {
        def expr = 
            Expr.Operation(
                "*",
                Expr.Const(10),
                Expr.Operation(
                "+",
                Expr.Var("n"),
                Expr.Const(5)
                )
            );
        def operations = Dictionary.[string, double * double -> double]();
        operations["+"] = (x, y) => x + y;
        operations["*"] = _ * _;

        def vars = Dictionary();
        vars["n"] = 3.0;

        def result = expr.Eval(operations, vars);
        WriteLine($"Result is $result");
    }
}

I know little about Nemerle, but I think one of its big features is macros (a la hygenic Scheme-like happy macros, as opposed to ugly C-like macros). 我对Nemerle知之甚少,但我认为它的一大特色是宏(一种类似于hyound Scheme的快乐宏,而不是丑陋的C类宏)。 I never quite grokked why people love macros so much, but then again, I never grokked why people like algebraic data types and pattern-matching so much, until I started using F#. 我从来没有完全理解为什么人们如此喜欢宏,但是再一次,我从来没有理解为什么人们喜欢代数数据类型和模式匹配这么多,直到我开始使用F#。 So I suspect that if you love macros, and you use .NET, then you're a rabid Nemerle fan. 所以我怀疑如果你喜欢宏,并且你使用.NET,那么你就是一个狂热的Nemerle粉丝。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM