Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
F# 为什么不是';我的函数上没有识别出元组参数吗?_F# - Fatal编程技术网

F# 为什么不是';我的函数上没有识别出元组参数吗?

F# 为什么不是';我的函数上没有识别出元组参数吗?,f#,F#,为什么我的函数不能识别元组参数? 我有以下功能: let deposit depositType ((logTransaction:string -> string -> unit), (file:string)) = depositType |> makeInitialDeposit |> sprintf "Deposited: %f" |> logTransaction file 请注意,

为什么我的函数不能识别元组参数?

我有以下功能:

let deposit depositType ((logTransaction:string -> string -> unit), (file:string)) =
    depositType |> makeInitialDeposit
                |> sprintf "Deposited: %f"
                |> logTransaction file
请注意,函数的最后一个参数是一个元组:

((logTransaction:string -> string -> unit), (file:string))
然后,我尝试使用以下命令调用此函数:

let file = "c:\\myfile.txt"
(writeToFile, file) ||> deposit OneDollarBill
然而,它声明它并不期待一个元组。相反,它期望:

应为(字符串->字符串->单位)->字符串->a

完全错误如下:

类型不匹配。期待 (字符串->字符串->单位)->字符串->a但给定 (字符串->字符串->单位)*字符串->单位类型“字符串->字符串->单位”与类型(字符串->字符串->单位)*字符串”不匹配

代码如下:

let writeToFile (filePath:string) (message:string) =
    let file = new System.IO.StreamWriter(filePath)
    file.WriteLine(message)
    file.Close()

let makeInitialDeposit deposit =
    deposit |> insert []

let deposit depositType ((logTransaction:string -> string -> unit), (file:string)) =
    depositType |> makeInitialDeposit
                |> sprintf "Deposited: %f"
                |> logTransaction file

let file = "c:\\myfile.txt"
(writeToFile, file) ||> deposit OneDollarBill

||>
解压元组
'a*'b
以调用普通函数
'a->'b->'c
;如图所示,您不需要这种解包行为,因为您有
'a*'b->'c

||>
更改为
|>
,以便直接传递元组:

(writeToFile, file) |> deposit OneDollarBill
或者将
deposit
更改为接受curried参数而不是元组:

let deposit depositType (logTransaction:string -> string -> unit) (file:string) =
    ...

(writeToFile, file) ||> deposit OneDollarBill

||>
解压元组
'a*'b
以调用普通函数
'a->'b->'c
;如图所示,您不需要这种解包行为,因为您有
'a*'b->'c

||>
更改为
|>
,以便直接传递元组:

(writeToFile, file) |> deposit OneDollarBill
或者将
deposit
更改为接受curried参数而不是元组:

let deposit depositType (logTransaction:string -> string -> unit) (file:string) =
    ...

(writeToFile, file) ||> deposit OneDollarBill

我的建议是不要使用
| |>
操作符。我知道F#社区的一些人喜欢它,但我几乎从不使用它,因为我认为它不会使代码更可读。您需要更加明确,但我认为没有
|124;>
的代码更容易理解:)@TomasPetricek:IMO调用fold的情况下,
|124;>
总是更好,因为当您从前面导入状态时,类型推断工作得更好。我认为禁止
|124;>
并不能解决问题。它的语法很简单,用途也很有效。如果这足以引起混乱,那么是时候问问为什么了。斯科特,你有时问的问题在相关定义中有直截了当的答案。花一些额外的时间学习所用特性的确切定义和功能可能会使您的编程更加高效,这样您就可以更加自信地确定问题的根源。当完全清楚函数、参数、元组和管道是如何工作的时,
| |>
的奇怪之处就变得更加突出了。谢谢Vandroy。你完全正确。我在过去(即几个月前)使用过该运算符,并认为我知道如何使用它。我的建议是不要使用
|124;>
运算符。我知道F#社区的一些人喜欢它,但我几乎从不使用它,因为我认为它不会使代码更可读。您需要更加明确,但我认为没有
|124;>
的代码更容易理解:)@TomasPetricek:IMO调用fold的情况下,
|124;>
总是更好,因为当您从前面导入状态时,类型推断工作得更好。我认为禁止
|124;>
并不能解决问题。它的语法很简单,用途也很有效。如果这足以引起混乱,那么是时候问问为什么了。斯科特,你有时问的问题在相关定义中有直截了当的答案。花一些额外的时间学习所用特性的确切定义和功能可能会使您的编程更加高效,这样您就可以更加自信地确定问题的根源。当完全清楚函数、参数、元组和管道是如何工作的时,
| |>
的奇怪之处就变得更加突出了。谢谢Vandroy。你完全正确。我在过去(即几个月前)使用过该操作符,并认为我知道如何使用它。