我认为没有很好的方法将myfunc 的参数线程化到func1 和func2 的参数。但是,您的示例可能有点过于简单,因为您可以只写:
let myfunc x y =
func1 x y && func2 x y
我想实际上,你有更多的功能,然后使用fold 是很有意义的。在这种情况下,您可以使用fold 来组合函数,而不是使用它来组合结果。
如果我稍微简化问题并假设func1 和func2 将这两个参数作为一个元组(而不是将它们作为两个单独的参数),那么您可以编写如下内容:
let func1 (a, b) = true
let func2 (a, b) = true
let myfunc = List.fold (fun f st a -> f a && st a) (fun _ -> true) [ func1; func2 ]
现在您不需要显式地传递参数(到func1 和func2),但是fold 的参数有点复杂。我认为这很好,因为您只需要编写一次(而且这样可读性很强)。
但是,如果你是无点风格的粉丝(或者只是想看看你能走多远),你可以定义一些辅助函数,然后编写如下代码:
/// Given a value, returns a constant function that always returns that value
let constant a _ = a
/// Takes an operation 'a -> b -> c' and builds a function that
/// performs the operation on results of functions
let lift2 op f g x = op (f x) (g x)
let myfunc2 = List.fold (lift2 (&&)) (constant true) [ ffunc1; ffunc2 ]
如果您不需要任意数量的函数,那么我会简化代码并且根本不使用fold。如果您需要这样做,那么我认为您的版本非常可读且不会太长。我在这个答案中写的例子表明你可以避免手动传递参数,但是它使代码有点神秘。