简体   繁体   English

F# 语法糖

[英]F# syntactic sugar

F# clearly contains a lot of things that are syntactic sugar, and as I try to learn it--without the aid of a book--I am overwhelmed by the sheer variety of syntax. F# 显然包含很多语法糖,当我尝试学习它时——没有书的帮助——我被各种各样的语法所淹没。 Is there a simpler "core" language hidden behind all that syntactic sugar?在所有语法糖的背后是否隐藏着一种更简单的“核心”语言? Is there a cheat sheet list of the syntactic sugars and how they map to the core language?是否有语法糖的备忘单列表以及它们如何 map 到核心语言?

And hey, is there a reason F# requires a different "assignment" operator for function definitions than for lambdas, or was it a random decision?嘿,F# 对 function 定义需要与 lambda 不同的“赋值”运算符,这是有原因的,还是随机决定? eg "let inc x = x+1" vs "fun x -> x+1"例如“让公司 x = x+1”与“有趣的 x -> x+1”

A great deal of F# syntax comes from OCaml - many OCaml programs will compile and run in F# with no changes.大量的 F# 语法来自 OCaml - 许多 OCaml 程序将在 F# 中编译和运行而无需更改。

Good starter links:好的入门链接:

http://msdn.microsoft.com/en-us/fsharp/default.aspx http://msdn.microsoft.com/en-us/fsharp/default.aspx

http://en.wikibooks.org/wiki/F_Sharp_Programming http://en.wikibooks.org/wiki/F_Sharp_Programming

http://lorgonblog.spaces.live.com/blog/cns.701679AD17B6D310?887.entry?_c=BlogPart http://lorgonblog.spaces.live.com/blog/cns.701679AD17B6D310?887.entry?_c=BlogPart

Along with Brian's links, I'd like to pass along links to my own blog series, Why I Love F# , which covers some of the basics.除了 Brian 的链接,我还想传递我自己的博客系列的链接,为什么我喜欢 F# ,它涵盖了一些基础知识。

With regard to your second question, "let inc x = x+1" vs "fun x -> x+1" are two ways of expressing a function, but only the first one uses the assignment operator.关于你的第二个问题,“let inc x = x+1”与“fun x -> x+1”是表达 function 的两种方式,但只有第一个使用赋值运算符。 In fact,实际上,

let inc x = x + 1

Is equivalent to:相当于:

let inc = (fun x -> x + 1)

The first is shorthand for the second, but the second might illuminate how all functions in F# are really lambdas bound to names.第一个是第二个的简写,但第二个可能说明 F# 中的所有函数实际上是绑定到名称的 lambda。

F#, barring .NET interop, seems quite simpler than, say C#. F#,除了 .NET 互操作之外,似乎比 C# 更简单。 (Not really picking on it in particular, but since it's another popular .NET language...) (并没有特别挑剔它,但因为它是另一种流行的 .NET 语言......)

Take F# functions, for instance:以 F# 函数为例:

  • There's no magic "void" type-thats-not-really-a-type没有神奇的“空”类型——那不是真正的类型
    • Hence everything is an expression因此,一切都是一种表达
  • All functions are unary所有功能都是一元的
  • Functions are first class type功能先class型

Right there you have a function system that's vastly easier than, say, C#, where void creates a special case, and there's no generalization possible over all functions.在那里,您有一个 function 系统,它比 C# 容易得多,其中 void 创建了一个特殊情况,并且不可能对所有函数进行泛化。

As to your specific question, with "let fx = x" versus "let f = fun x -> x", that's probably an inherited trait from ML.至于您的具体问题,“let fx = x”与“let f = fun x -> x”,这可能是 ML 的继承特征。 (I don't see any particular reason why it couldn't be "let f = fun x = x", except that perhaps it'd be more confusing and perhaps make the grammar more complex?. (Personally I'd prefer "let f = \xx".)) Anyways, while in most cases they are equivalent, sometimes you must define a syntactic function instead of a function value. (我看不出它不能是“let f = fun x = x”的任何特殊原因,只是它可能会更令人困惑并且可能会使语法更复杂?。(我个人更喜欢“ let f = \xx".)) 无论如何,虽然在大多数情况下它们是等价的,但有时您必须定义语法 function 而不是 function 值。

.NET interop can, unfortunately, make things a bit more complicated, although probably not more or much more than other .NET languages.不幸的是,.NET 互操作可以使事情变得更复杂一些,尽管可能不会比其他 .NET 语言更多或更多。

Only one of the two expressions you mentioned uses an assignment operator.您提到的两个表达式中只有一个使用赋值运算符。

This defines a function bound to the identifier "inc" which takes a single argument of type int and returns the argument +1.这定义了一个绑定到标识符“inc”的 function,该标识符接受一个 int 类型的参数并返回参数 +1。

let inc x = x + 1;

This on the other hand, is an expression which represents a lambda.另一方面,这是一个表示 lambda 的表达式。 It is bound to no identifier cannot exist solely by itself.它绑定到任何标识符不能单独存在。 It must be part of a larger expression.它必须是更大表达式的一部分。

fun x -> x + 1

If you are looking for a cheat sheet for F#, this is the one that I use on a regular basis.如果您正在寻找 F# 的备忘单,这是我经常使用的备忘单。

fsharpcheatsheet.pdf fsharpheatsheet.pdf

It doesn't cover everything, things like Units of Measure or quotations, and doesn't talk about the various libraries, but it does cover a lot of the syntax.它没有涵盖所有内容,例如度量单位或引用,也没有讨论各种库,但它确实涵盖了很多语法。

http://www.samskivert.com/code/fsharp/fsharp-cheat-sheet.pdf http://www.samskivert.com/code/fsharp/fsharp-cheat-sheet.pdf

It's a pretty comprehensive cheat sheet that has helped me out a lot with learning F#这是一个非常全面的备忘单,对我学习 F# 有很大帮助

Notice that you can apply directly a lambda in an expression.请注意,您可以在表达式中直接应用 lambda。

let i = (fun x -> x + 1)0  //i equal 1

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

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