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

问题描述 投票:1回答:3

我正在使用FSI,我如何从bool转到浮动?

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

你不能将bool转换为浮动吗?有什么资源可以理解签名吗?我找不到bool去做任何事情的任何例子,我需要更好地理解“currying”吗? (我对函数式编程完全不熟悉)

f#
3个回答
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

来自迈克尔的神奇解决方案通过使用模式(匹配具体值)来避免命名参数,这为您提供了正确的签名,但也提供了大量警告,因为如果使用任何其他值作为参数调用函数将失败:

> 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

2
投票

设f 7为真0.3 =“完成”;;

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

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


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.