Nemerle 和 F# 在 .Net 上的功能比较

发布于 2024-09-16 00:47:25 字数 400 浏览 5 评论 0原文

社区 Wiki 问题:

根据此问题:在 .Net 中使用 Scala 的好处是什么? 我想到了另一个问题。谁能列出 Nemerle 和 F# 在 .Net 平台上进行功能开发的比较优势(和劣势)?我只是顺便看了一眼内梅尔。听起来它与 F# 的运行方式大致相同,所以我想知道除了明显的语法差异以及 F# 得到 Microsoft 支持的巨大优势之外,还有哪些差异。

Community Wiki Question:

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月下伊人醉 2024-09-23 00:47:25

我接触过这两种语言,我对 Nemerle 的印象简要如下:(我假设大多数观众都熟悉 F#,而 Nemerle 不太受欢迎,所以为了公平起见,我会多介绍一些):

  • F# 社区相当大,并且由于大量的博客文章、文章等而不断增长。而且它分布在各个国家/地区。相反,Nemerle 爱好者基本上都讲俄语,并且集中在 RSDN.ru 网站上。
  • 在我看来,Nemerle 语法对于具有类似 C 语言背景的开发人员来说更加友好。
  • Nemerle(以及 F#)具有类型推断功能。 Nemerle 中的类型推断机制绑定到方法体(局部函数、变量等),与 F# 全局类型推断作用域相反。然而,Nemerle 编译器并不强制执行任何特定的代码编写习惯来协助类型推断机制。

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

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# 使用基于 Hindley-Milner 算法的方法并尝试推断最通用的类​​型。相反,Nemerle 从不推断多态类型,并且总是寻找最具体的类型。

F#

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

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

run addInt 5
run addString "S"

Nemerle 在相同条件下会将 run 类型推断为 (int->int) * int ->;国际。

有关 Nemerle 类型推理机制的更多详细信息,请参阅 Michal Moskal 的硕士论文:带有延迟的类型推断

  • Nemerle 具有丰富的元编程功能。大多数语言控制结构,如循环、条件表达式、LINQ 支持、即将推出的解析功能(包括 C# 源代码等等)——所有这些都是使用宏创建的。可以在此处找到宏应用程序示例。顺便说一句,上面示例中使用 $ 语法的字符串格式化功能 - 也是内置宏。

编辑:添加了稍大的样本

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’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# 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

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. 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 in the same conditions will infer type of run as (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 has rich metaprogramming capabilities. 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. 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.

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");
    }
}
指尖上得阳光 2024-09-23 00:47:25

我对 Nemerle 知之甚少,但我认为它的一大特点是宏(一个卫生的类似计划的快乐宏,而不是丑陋的类似 C 的宏)。我从来没有真正理解为什么人们如此喜欢宏,但话又说回来,我从来没有真正理解为什么人们如此喜欢代数数据类型和模式匹配,直到我开始使用 F#。所以我怀疑,如果您喜欢宏并且使用 .NET,那么您就是 Nemerle 的狂热粉丝。

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). 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#. So I suspect that if you love macros, and you use .NET, then you're a rabid Nemerle fan.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文