我正在使用FSI,我如何从bool转到浮动?
你不能将bool转换为浮动吗?有什么资源可以理解签名吗?我找不到bool去做任何事情的任何例子,我需要更好地理解“currying”吗? (我对函数式编程完全不熟悉)
除非这是一个难题或挑战,你可以假设i:int -> b:bool -> f:float -> string
与int -> 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
设f 7为真0.3 =“完成”;;
val f:int - > bool - > float - > string
具有签名val sigF : int -> bool -> float -> string
的函数可以是例如let sigF (i:int) (b:bool) (f:float) = ""
。 ->
符号行中的最后一个是输出,其他所有符号都是输入。