什么 - >在F#中意味着什么? [英] What does -> mean in F#?

查看:139
本文介绍了什么 - >在F#中意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图进入F#开关一段时间,但我一直在推迟。为什么?

因为无论我尝试查看哪些'初学者'资源,我都会看到非常简单的例子,它们开始使用运算符 - >



然而,我还没有找到任何可以清楚简单地解释这个操作符意味着什么的方法。好像它一定很明显,甚至不需要解释,甚至完成新手。



因此,我必须非常密集或者可能是将近30年的经验让我回来。





$ b

解决方案

' - >'不是一个运算符。它出现在许多地方的F#语法中,其含义取决于它如何用作更大构造的一部分。



在类型中,' - > '描述了上面描述的功能类型。例如

  let f:int  - > int = ... 

表示'f'是一个函数,它接受一个int并返回一个int 。

在lambda(以'fun'关键字开头的东西)中,' - >'是将参数从正文中分离出来的语法。例如

  fun x y  - > x + y + 1 

是一个用给定的实现定义两个参数函数的表达式。 p>

在匹配结构中,' - >'是将模式与模式匹配时应运行的代码分离的语法。例如,在

 匹配someList和
| [] - > 0
| h :: t - > 1

每个' - >'左边的东西都是图案,右边的东西如果左边的模式匹配,会发生什么情况。



理解的困难可能源于错误的假设,即' - >'是运算符单一的意思。类比可能是。在C#中,如果你以前从未见过任何代码,并尝试分析。运算符基于查看obj.Method和3.14和System.Collections,您可能会感到非常困惑,因为符号在不同的上下文中具有不同的含义。然而,一旦你知道足够的语言来识别这些上下文,事情就会变得清晰。

I've been trying to get into F# on and off for a while but I keep getting put off. Why?

Because no matter which 'beginners' resource I try to look at I see very simple examples that start using the operator ->.

However, nowhere have I found as yet that provides a clear simple explanation of what this operator means. It's as though it must be so obvious that it doesn't need explanation even to complete newbies.

I must therefore be really dense or perhaps it's nearly 3 decades of previous experience holding me back.

Can someone please, explain it or point to a truly accessible resource that explains it?

解决方案

'->' is not an operator. It appears in the F# syntax in a number of places, and its meaning depends on how it is used as part of a larger construct.

Inside a type, '->' describes function types as people have described above. For example

let f : int -> int = ...

says that 'f' is a function that takes an int and returns an int.

Inside a lambda ("thing that starts with 'fun' keyword"), '->' is syntax that separates the arguments from the body. For example

fun x y -> x + y + 1

is an expression that defines a two argument function with the given implementation.

Inside a "match" construct, '->' is syntax that separates patterns from the code that should run if the pattern is matched. For example, in

match someList with
| [] -> 0
| h::t -> 1

the stuff to the left of each '->' are patterns, and the stuff on the right is what happens if the pattern on the left was matched.

The difficulty in understanding may be rooted in the faulty assumption that '->' is "an operator" with a single meaning. An analogy might be "." in C#, if you have never seen any code before, and try to analyze the "." operator based on looking at "obj.Method" and "3.14" and "System.Collections", you may get very confused, because the symbol has different meanings in different contexts. Once you know enough of the language to recognize these contexts, however, things become clear.

这篇关于什么 - >在F#中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆