[英]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# 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论文中找到: 推断类型推断
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.