F# - 没有参数的函数? [英] F# - Function with no arguments?

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

问题描述

当以功能性思维方式思考时,考虑到函数应该是纯粹的,可以断定任何没有参数的函数基本上只是一个值。

然而,reallity阻碍了,并且不同的输入,我可能不需要某个函数,如果该函数的计算成本很高,我不想评估它,如果不需要。

我找到了一个解决方法,使用让func _ = ... 并用func 1或其他函数调用它,但是这对读者来说非常不习惯和混乱。

When thinking in a functional mindset, given that functions are supposed to be pure, one can conclude any function with no arguments is basically just a value.
However, reallity gets in the way, and with different inputs, I might not need a certain function, and if that function is computationally expensive, I'd like to not evaluate it if it's not needed.
I found a workaround, using let func _ = ... and calling it with func 1 or whatever, but that feels very non-idiomatic and confusing to the reader.

这可归结为一个问题:在F#中,是否有一种正确的方法来声明一个带有零参数的函数,而不需要在声明上进行评估? b $ b

This boils down to one question: In F#, Is there a proper way to declare a function with zero arguments, without having it evaluated on declaration?

推荐答案

通常的习惯用法是定义函数来接受一个类型为Unit的参数( let functionName()= 42 )。它将被称为 functionName()。 (单元类型只有一个值,它是()。)

The usual idiom is to define the function to take one argument of type Unit (let functionName () = 42). It will then be called as functionName (). (The unit type has only one value, which is ().)

这篇关于F# - 没有参数的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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