我如何在f#中获得此签名:val sigF:int-> bool-> float-> string

时间:2019-03-13 14:31:05

标签: f#

我正在使用FSI,如何从布尔值转换为浮动值?

  • 有:let sigF 1 = 1 = 1得到:val sigF:int-> bool
  • 需要具有签名的函数:val sigF:int-> bool-> float-> string

您不能将bool转换为float吗? 在理解签名方面有任何资源吗?我找不到布尔在其他地方有用的示例,我是否需要更好地理解“咖喱”? (我对函数式编程完全陌生)

3 个答案:

答案 0 :(得分:3)

除非这是一个难题或挑战,否则您可以假设i:int -> b:bool -> f:float -> stringint -> bool -> float -> string相同。唯一的区别是,前者还包括函数参数的名称-这只是您可以忽略的额外信息,其含义并没有太大改变。

如果执行以下操作,则将获得参数名称:

> let sigF (i:int) (b:bool) (f:float) = "";;
val sigF : i:int -> b:bool -> f:float -> string

Michael的原始解决方案避免通过使用模式(与具体值匹配)来命名参数,这给了您正确的签名,但同时也有很多警告,因为如果以任何其他值作为参数调用该函数都会失败:

> let sigF 7 true 0.3 = "done";;

warning FS0025: Incomplete pattern matches on this expression. 
  For example, the value '0.0' may indicate a case not covered by the pattern(s).
warning FS0025: Incomplete pattern matches on this expression. 
  For example, the value 'false' may indicate a case not covered by the pattern(s).
warning FS0025: Incomplete pattern matches on this expression. 
  For example, the value '0' may indicate a case not covered by the pattern(s).

val sigF : int -> bool -> float -> string

为您提供正确签名但又没有警告的另一种解决方案是使用带有类型注释的_模式-这表示您忽略了argumnet,但为它提供了显式类型:

> let sigF (_:int) (_:bool) (_:float) = "";;
val sigF : int -> bool -> float -> string

答案 1 :(得分:2)

让f 7 true 0.3 =“ done” ;;

  • 7为true和.03(均表示函数输入),而=后为输出

val f:int-> bool-> float->字符串

答案 2 :(得分:0)

具有签名val sigF : int -> bool -> float -> string的功能可以是例如let sigF (i:int) (b:bool) (f:float) = ""->行中的最后一个是输出,所有其他都是输入。

相关问题