本文介绍了含义关键字"而在"在F#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始学习F#。在几个F#的编码的例子我看到按以下方式使用的中的关键字:

I am just starting to learn F#. In several F# coding examples I see the keyword "in" used in the following way:

let doStuff x =
    let first, second = x in
    first + " " + second

的功能的工作原理使用和不使用,在在随后的第二行结束。什么是中吗?

The function works with and without the "in" at then end of the second line. What does "in" do?

推荐答案

是F#的OCaml的根解酒它指定的的约束的变量,这是微妙的不同变量的范围的。

in is a hangover from F#'s OCaml roots and it specifies bound variables, which are subtly different to variable scopes.

想想变量绑定如下;你有一个前pression:

Think of variable binding as follows; You have an expression:

first + " " + second

由于它代表第一第二是绑定 - 他们没有任何固定值 - 让前pression具有present没有具体的价值。通过使用

As it stands first and second are unbound - they don't have any fixed values - so that expression has no concrete value at present. By using

let (...) in

语法要指定这些变量是如何在EX pression 绑定的,所以你的例子将使用变量替换,以减少功能下降到

syntax you are specifying how those variables are bound in that expression, so your example will use variable substitution to reduce that function down to

let doStuff x =
  x + " " + x



在这个例子中这两种形式是相同的,但是想象一下以下内容:

In this example both forms are identical, but imagine the following:

let (x = 2 and y = x + 2) in
     y + x

这是行不通的一样

let (x = 2 and y = x + 2)
     y + x

由于在前者的情况下 X 只能被绑定的关键字。

Because in the former case x is only bound after the in keyword.

在后一种情况下正常变量的作用域的规则生效,因此变量一旦它们被声明的约束。

In the later case normal variable scoping rules take effect, so variables are bound as soon as they are declared.

希望扫清事情了。一般来说,你应该总是使用版本的没有并指定 #light 在开始您的F#源文件

Hope that clears things up. In general you should always use the version without in and specify #light at the start of your F# source files

这篇关于含义关键字"而在"在F#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-25 02:23