本文介绍了中Nemerle和F#的比较进行功能上的.Net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

社区维基问题:

根据这样一个问题:的的另一个问题浮现在脑海。任何人都可以制定出了 Nemerle 和F#的比较优势(和缺点)的.NET平台上的功能开发?我只是看着Nemerle一笔带过。这听起来有点像它在同一个球场为F#,所以我想知道什么区别有较明显的语法差异和很大的优势F#有被支持微软等。

Pursuant to this question: What are the benefits of using Scala in .Net? 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? I've just looked at Nemerle in passing. 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.

推荐答案

我感动了这两种语言和我的Nemerle IM pressions简述如下:(我认为大多数观众熟悉F#和Nemerle是不太受欢迎的,所以为公平起见,我将介绍这一点):

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):

  • 在F#社区是相当大的,它不断地增长,由于大量blogposts的,文章等。此外,它是整个国家的小号preaded。由于相反,Nemerle爱好者基本上都是讲俄语,并集中在 RSDN.ru 网站。
  • Nemerle语法为背景开发的类C语言IMO友好得多。
  • 在Nemerle(以及F#)的类型推断功能。在Nemerle类型推理机制,势必方法体(局部函数,变量等),反对F#全局类型推断范围。然而Nemerle编译器不会强制写作code任何具体的成语,以协助类型推理机制。
  • F# community is rather big and it grows constantly due to large number of blogposts, articles etc. Also it is spreaded across the countries. As the opposite, Nemerle enthusiasts are basically russian-speaking and concentrated on RSDN.ru site.
  • Nemerle syntax is IMO much friendlier for developers with background in C-like languages.
  • Nemerle (as well as F#) has type inference features. Type inference mechanism in Nemerle is bound to method body (local functions, variables and so on), opposing to F# global type inference scope. However Nemerle compiler doesn’t enforce any specific idioms of writing code to assist type inference mechanism.

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]

此外,您可能会注意到Nemerle编译器的另一个特点 - 它可以进一步使用推断类型。为了演绎出类型F#使用基于辛德米尔纳算法的方法,并试图推断出最通用的类​​型。 Nemerle,相反,从未推断出多态类型,总是寻找最具体类型。

Also you may notice another feature of Nemerle compiler – it can infer types from further usage. To deduce types F# uses approach based on Hindley-Milner algorithm and tries to infer most generic type. Nemerle, in opposite, never infers polymorphic types and always looks for most specific type.

F#

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

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

run addInt 5
run addString "S"

Nemerle在同等条件下将推断类型的运行作为(内部 - > INT)* INT - > INT

Nemerle in the same conditions will infer type of run as (int->int) * int -> int.

在Nemerle类型推理机制的更多细节可以在论文米哈尔Moskal的硕士中找到:的

More details on Nemerle type inference mechanism can be found in MSc thesis of Michal Moskal: Type Inference With Deferral

编辑:新增稍大样品

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");
    }
}

这篇关于中Nemerle和F#的比较进行功能上的.Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!