这个'()'符号是什么意思? [英] What does this '()' notation mean?

查看:371
本文介绍了这个'()'符号是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始学习F#。本书使用以下表示法:

  let name()= 3 
name()
code>

与此不同:

  let name = 3 
name


在回答()之前,让我们定义一些基础知识并完成一些示例。


在F#中 let statement < a>有一个名称,零个或多个参数和一个表达式。



为了保持这个简单,我们将使用:

如果没有那么let语句是


如果有参数,那么let语句是一个函数


对于一个值,表达式的结果只计算一次并绑定到标识符;它是不可变的。

对于函数,每次调用函数时都会计算表达式。



所以这个

  let a = System.DateTime.Now ;; 

在第一次评估或稍后调用时总是有时间的,即

  a ;; 
val it:System.DateTime = 1/10/2017 8:16:16 AM ...
a ;;
val it:System.DateTime = 1/10/2017 8:16:16 AM ...
a ;;
val it:System.DateTime = 1/10/2017 8:16:16 AM ...

和这个函数

  let b()= System.DateTime.Now ;; 

每次评估时都会有一个新的时间,例如

  b();; 
val it:System.DateTime = 1/10/2017 8:18:41 AM ...
b();;
val it:System.DateTime = 1/10/2017 8:18:49 AM ...
b();;
val it:System.DateTime = 1/10/2017 8:20:32 AM ...

现在解释()的含义。注意 System.DateTime.Now 不需要任何参数。


我们创建一个函数,当表达式不需要参数?

每个参数必须有一个类型,所以F#有单元类型,用于不需要参数的函数和只有单位类型的值是()



所以这是一个带有一个参数<$ c $类型 int

 的类型c> x 让cx = x + 1 ;; 

这是一个带有一个参数()类型单元

  let b()= System.DateTime 。现在;; 


I just started to learn F#. The book uses the following notation:

let name() = 3
name()

what that differs from this:

let name = 3
name

?

解决方案

Before answering what () is lets get some basics defined and some examples done.

In F# a let statement has a name, zero or more arguments, and an expression.

To keep this simple we will go with:
If there are no arguments then the let statement is a value.
If there are arguments then the let statement is a function.

For a value, the result of the expression is evaluated only once and bound to the identifier; it is immutable.
For a function, the expression is evaluated each time the function is called.

So this value

let a = System.DateTime.Now;;

will always have the time when it is first evaluated or later invoked, i.e.

a;;
val it : System.DateTime = 1/10/2017 8:16:16 AM ...  
a;;
val it : System.DateTime = 1/10/2017 8:16:16 AM ...  
a;;
val it : System.DateTime = 1/10/2017 8:16:16 AM ...  

and this function

let b () = System.DateTime.Now;;

will always have a new time each time it is evaluated, i.e.

b ();;
val it : System.DateTime = 1/10/2017 8:18:41 AM ...  
b ();;
val it : System.DateTime = 1/10/2017 8:18:49 AM ...  
b ();;
val it : System.DateTime = 1/10/2017 8:20:32 AM ... 

Now to explain what () means. Notice that System.DateTime.Now needs no arguments to work.

How do we create a function when the expression needs no arguments?

Every argument has to have a type, so F# has the unit type for functions that need no arguments and the only value for the unit type is ().

So this is a function with one argument x of type int

let c x = x + 1;;

and this is a function with one argument () of type unit

let b () = System.DateTime.Now;;

这篇关于这个'()'符号是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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